Skip to content
Permalink
Browse files

Add ability to pass the context that the callback should be run in.

  • Loading branch information...
gcorne committed Jan 29, 2014
1 parent 60707d1 commit e7ab26d9aae2f5383b8ec156454ce241bcb4a87e
Showing with 27 additions and 7 deletions.
  1. +8 −7 src/event-manager.js
  2. +19 −0 test/qunit-test.js
@@ -35,10 +35,10 @@
* @param callback Must be a valid callback function before this action is added
* @param priority Defaults to 10

This comment has been minimized.

Copy link
@carldanley

carldanley Jan 29, 2014

Needs documentation for the new parameter.

*/
function addAction( action, callback, priority ) {
function addAction( action, callback, priority, context ) {
if( typeof action === 'string' && typeof callback === 'function' ) {
priority = parseInt( ( priority || 10 ), 10 );
_addHook( 'actions', action, callback, priority );
_addHook( 'actions', action, callback, priority, context );
}

return MethodsAvailable;
@@ -79,7 +79,7 @@
* @param callback Must be a valid callback function before this action is added
* @param priority Defaults to 10

This comment has been minimized.

Copy link
@carldanley

carldanley Jan 29, 2014

Needs documentation for the new parameter.

*/
function addFilter( filter, callback, priority ) {
function addFilter( filter, callback, priority, context ) {
if( typeof filter === 'string' && typeof callback === 'function' ) {
priority = parseInt( ( priority || 10 ), 10 );
_addHook( 'filters', filter, callback, priority );
@@ -138,10 +138,11 @@
* @param priority The priority of this hook. Must be an integer.
* @private

This comment has been minimized.

Copy link
@carldanley

carldanley Jan 29, 2014

Needs documentation for the new parameter.

*/
function _addHook( type, hook, callback, priority ) {
function _addHook( type, hook, callback, priority, context ) {
var hookObject = {
callback : callback,
priority : priority
priority : priority,
context : context
};

// Utilize 'prop itself' : http://jsperf.com/hasownproperty-vs-in-vs-undefined/19
@@ -198,10 +199,10 @@

for( var i = 0, len = hooks.length; i < len; i++ ) {
if( type === 'actions' ) {
hooks[ i ].callback.apply( undefined, args );
hooks[ i ].callback.apply( hooks[ i ].context, args );
}
else {
args[ 0 ] = hooks[ i ].callback.apply( undefined, args );
args[ 0 ] = hooks[ i ].callback.apply( hooks[ i ].context, args );
}
}

@@ -147,3 +147,22 @@ test( 'fire action multiple times', function() {
wp.hooks.doAction( 'test.action' );
wp.hooks.removeAction( 'test.action' );
} );

test( 'fire action using method with context', function() {
var obj;
expect(1);

obj = {
foo: 10,
method: function() {
equal( this.foo, 10 );
}
};

wp.hooks.addAction( 'test.action', obj.method, 10, obj );

wp.hooks.doAction( 'test.action' );
wp.hooks.removeAction( 'test.action' );


} );

2 comments on commit e7ab26d

@carldanley

This comment has been minimized.

Copy link

replied Jan 29, 2014

Looks good! Just waiting on documentation.

@gcorne

This comment has been minimized.

Copy link
Owner Author

replied Jan 29, 2014

See a294340 :)

Please sign in to comment.
You can’t perform that action at this time.