[wp-trac] [WordPress Trac] #21170: JavaScript actions and filters

WordPress Trac wp-trac at lists.automattic.com
Sun Jul 15 06:43:17 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 azaozz):

 Replying to [comment:30 carldanley]:
 > ... they (actions and filters) are essentially treated the same, except
 for how they are processed; the only slight variance is that filters can
 modify the value passed to them.

 Right, they are handled the same way in PHP too. Essentially they are the
 same thing except for filters the returned value of each callback is
 passed on to the next callback as first arg, and returned at the end.

 > Are you referencing how the objects for filters & actions are bound with
 3 properties ( id, callbacks & children ) and sort of nested within each
 other?

 Uh, sry I didn't explain that well. Was referring to support for more than
 one namespace/identifier, like: `actionname.namespace.other.third`. When
 parsed that would create a tree-like structure for each action that
 doesn't seem needed as all actions would be called regardless of the
 structure. So a "flat" structure, i.e. one branch (actionname) with many
 leaves (callbacks) would work just as well. The .namespace/identifier bit
 would only be used to identify the callback, so each "leaf" on the
 "branch" would have a name and we would be able to easily remove anonymous
 "leaves" by branch name + leaf name :)

 (In 21170.patch the term "namespace" should be read "identifier" as that
 probably describes it's role better).

 Also thinking the namespaces/identifiers should be required as that would
 make removing anonymous functions easy. As far as I see the most common
 use of this would be with anonymous functions: `addAction('init.my-thing',
 function(){...});

 > What if we did the following:
 >  * rework the internal storage to have 2 variables or keys for 'actions'
 and 'filters'.

 Actions and filters should be kept separate, i.e. `addAction('init')`
 makes this an "action node", there can't be a filter 'init',
 `applyFilters('something', val)` would be another node, etc. If we create
 the nodes as instances of a small class, or even as simple objects, we can
 store the nodeType in each.

 >  * when addAction() is called with a namespace, split it and find every
 hierarchical identifier to check to see if they exist, creating them, if
 they don't already exist, as you go, ie. you add "my.awesome.func", build
 an array of "levels" of identifiers ( [ 'my', 'my.awesome',
 'my.awesome.func' ] ) and then loop through the array.

 Not sure there's a need for that hierarchy (as explained above). Perhaps
 some usage examples would help here.

 > ...I think for this particular case chaining makes good sense when doing
 something. In terms of useability, I'd rather see/type
 > {{{
 > $wp.hooks.addAction().addAction().addFilter().addFilter();
 > }}}

 Yeah, chaining is definitely handy. The question is whether it's better to
 support chaining or to return true/false on success/failure when adding or
 removing actions and filter. The example in 21170.patch (and
 jQuery.Callbacks() I believe) would only add a particular callback once,
 so returning false on attempts to add it again makes sense. I'm 50/50 on
 this at the moment, would be good to get more opinions :)

 > What do you think? I appreciate the feedback and am willing to rework
 logic on this. I've been pumped ever since lgedeon mentioned this ticket
 to me.

 Yes, it's an interesting challenge. Perhaps if we get it right, it can
 also be released as a stand-alone JS class.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21170#comment:31>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list