[wp-trac] [WordPress Trac] #21170: JavaScript actions and filters
WordPress Trac
wp-trac at lists.automattic.com
Mon Jul 16 22:35:45 UTC 2012
#21170: JavaScript actions and filters
----------------------------+--------------------------
Reporter: koopersmith | Owner: koopersmith
Type: task (blessed) | Status: new
Priority: normal | Milestone: 3.5
Component: General | Version: 3.4
Severity: normal | Resolution:
Keywords: |
----------------------------+--------------------------
Comment (by CaptainN):
I added namespace support to SignalsLite.js (all unit tested):
It works like this:
'''
// setup in core somewhere, or elsewhere
var actions = {
// simple factory method
addAction: function( name ) {
this[ name ] = new SignalLite( this );
}
}
// wp core or someone else, adds the action hooks
actions.add( 'wpAction' );
actions.wpAction.ns.add( 'myNamespace' );
// now the namespaces actions can be used
actions.wpAction.myNamespace.add( function() {
// do something with the action
} );
// later when it comes time to cleanup:
actions.wpAction.myNamespace.remove();
'''
I didn't add a way to dispatch a Signal only to a namespaced sub-set of
listeners, because it didn't seem to make sense to expose that kind of
functionality. If a listener was added to a Signal, it seems inappropriate
to only send the signal to certain listeners, it adds a second level of
subscriber sets, and that can be abused to basically duplicate the
dispatcher functionality, where there should probably be a second named
action - it may be better to encourage the creation of another distinct
hook.
I think event namespaces are mostly useful as a tool for plugin developers
to use to be able to clean themselves up, without having to retain a
reference to all the listeners they may add all over.
jQuery does support namespaced dispatching, and if there was a strong
desire for namespaced dispatching, it could be added easily to
SignalsLite.js. I'm not sure how often you'd have plugin or extension
developers triggering or dispatching actions and filters (like they
frequently do in jQuery), so it may not really be an issue, unless it's
wanted for core use.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/21170#comment:35>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list