Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom function for actions #116

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/createAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};

Expand All @@ -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);
};

Expand Down
21 changes: 21 additions & 0 deletions test/creatingActions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"});
Expand Down