javascript - Design pattern to support last-in callbacks (e.g. handling hashchange) -
on web page, have multiple independent plug-ins (for lack of better word) interact location.hash
of browser. (i have bit of code keeps work separated, that's irrelevant question.) example, have:
// file1.js $(window).bind('hashchange', function(){ ... } ); // file2.js $(window).bind('hashchange', function(){ ... } );
the above works desired when hash changes after page loads. however, need code process whatever hash page loads with. if write code:
// file1.js $(window).bind('hashchange', function(){ ... } ).trigger('hashchange'); // file2.js $(window).bind('hashchange', function(){ ... } ).trigger('hashchange');
...then first set of code run twice. not desirable.
what's pattern triggering hashchange
event once, after event handlers have been put in place?
one option namespace event handlers , triggers:
// file1.js $(window).bind('hashchange.file1', function(){...} ).trigger('hashchange.file1'); // file2.js $(window).bind('hashchange.file2', function(){...} ).trigger('hashchange.file2');
this cause both handlers run whenever generic hashchange
event occurs, cause namespaced handler run when triggered.
Comments
Post a Comment