From e5d5e3f47cf57fafb891e6087012388e2325f4de Mon Sep 17 00:00:00 2001 From: angus croll Date: Fri, 7 Nov 2014 15:28:46 -0800 Subject: [PATCH] support custom function for actions --- src/createAction.js | 4 ++-- test/creatingActions.spec.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/createAction.js b/src/createAction.js index baab88c..3c148cd 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -10,7 +10,7 @@ var _ = require('./utils'), * * @param {Object} definition The action object definition */ -module.exports = function(definition) { +module.exports = function(definition, customFunction) { definition = definition || {}; @@ -28,7 +28,7 @@ module.exports = function(definition) { _isAction: true },Reflux.PublisherMethods,definition); - var functor = function() { + var functor = customFunction || function() { functor[functor.sync?"trigger":"triggerAsync"].apply(functor, arguments); }; diff --git a/test/creatingActions.spec.js b/test/creatingActions.spec.js index 279401b..b854631 100644 --- a/test/creatingActions.spec.js +++ b/test/creatingActions.spec.js @@ -23,6 +23,27 @@ describe('Creating action', function() { assert.equal(action.random, def.random); }); + it("should use the customFunction if supplied",function(){ + var benchmark; + var fn = function() { + action.trigger(); + benchmark = Date.now(); + }; + var action = Reflux.createAction(null, fn); + assert.isFunction(action); + assert.isFunction(fn); + assert.equal(action, fn); + //make sure regular properties are still present + for(var apimethod in Reflux.PublisherMethods){ + assert.equal(Reflux.PublisherMethods[apimethod],action[apimethod]); + } + + //run the action + assert.isUndefined(benchmark); + action(); + assert.isNumber(benchmark); + }); + it("should throw an error if you overwrite any API other than preEmit and shouldEmit",function(){ assert.throws(function(){ Reflux.createAction({listen:"FOO"});