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

WordPress Trac wp-trac at lists.automattic.com
Thu Jul 12 21:12:36 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):

 Hmm, one thing I just noticed about SignalLite, and a lot of the other
 JavaScript callback systems, is they don't really provide a way to do
 WordPress style filters (other than having an additional whole set of
 callbacks). That is, send a value to the listener, and then deal with
 whatever the listener returns between each listener.

 It seems a JavaScript way to do something like that would be the DOM Event
 model of passing an object with some properties that can be used to apply
 the filter, then the system could just ignore return values. But that's
 not very WordPressy.

 Maybe the way to go is to add a return handler that the implementer is
 meant to support in the SignalLite constructor, if they choose:


 {{{
 var signaled = new SignalLite( null /* default target */,
     function( value ) {
         // do something with the returned value
     }
 );
 }}}

 This would actually create a property on the signal:

 {{{
 // you could also use this alternative syntax
 var signaled = new SignalLite();
 signaled.eachReturned = function( value ) {
     // do something with the returned value
 }
 }}}

 Here's what an implemented filter might look like:

 {{{
 /// in core/plugin code - implementation
 var value_tofilter = "some value of stuff";

 // use this or a factory method
 window.wpFilter = {
   value_filter: new SignalLite( null,
     function( value ) {
         value_tofilter = value;
     }
   )
 }

 // ... in client code - user of the filter
 wpFilter.value_filter.add( function( value ) {
     value.replace( "some", "THE" );
     return value;
 } );

 // ... back in core code - dispatching the filter
 value_tofilter = wpFilter.a_filter.dispatch( value_tofilter );

 console.log( value_tofilter );
 // LOG: THE value of stuff
 }}}

 I'll probably go ahead and implement that. :-)

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


More information about the wp-trac mailing list