diff --git a/package.json b/package.json index d906aa4..755d74a 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,12 @@ , "author": { "name": "Michael Wheeler" } -, "version": "0.0.3" +, "contributors": [ + { + "name": "Dave Timmerman" + } + ] +, "version": "0.0.4" , "licenses": [ { "type": "MIT" diff --git a/spec/stateMachineSpecs.js b/spec/stateMachineSpecs.js index 0be8808..365b21b 100644 --- a/spec/stateMachineSpecs.js +++ b/spec/stateMachineSpecs.js @@ -113,6 +113,38 @@ ok(_out); }); + test('enter method is called with event method parameters', function () { + var _in1, _in2; + newStates.build() + .state('simple', { + enter: function (str, data) { + _in1 = str; + _in2 = data; + } + }) + .event('withParams', 'young', 'simple') + ; + + newStates.withParams('a string', {some: 'data'}); + q.equal(_in1, 'a string'); + q.equal(_in2, {some: 'data'}); + }); + + test('eventName is added to enter method parameters', function () { + var _in; + newStates.build() + .state('simple', { + enter: function (str, data, name) { + _in = name; + } + }) + .event('withParams', 'young', 'simple') + ; + + newStates.withEventName('a string', {some: 'data'}); + q.equal(_in, 'simple'); + }); + test('does not overwrite existing properties', function () { var st = states(function () { this.state('simple'); diff --git a/stateMachine.js b/stateMachine.js index 43d4c84..c549f3c 100644 --- a/stateMachine.js +++ b/stateMachine.js @@ -61,7 +61,9 @@ function buildEventShortcut(name) { states[name] = states[name] || function () { - states.trigger(name); + var args = Array.prototype.slice.call(arguments); + var arr = args.sort(); + states.trigger(name, arr); }; } @@ -114,18 +116,19 @@ return curr && curr.name; } - , setState: function (name) { + , setState: function (name, args) { var prev = me.currentState(true); currentState = me._states[name]; - me.onChange(me.currentState(), prev.name); - is(currentState.enter, 'Function') && currentState.enter(); + me.onChange(me.currentState(), prev.name, args); + args.push(me.currentState()); is(prev.leave, 'Function') && prev.leave(); + is(currentState.enter, 'Function') && currentState.enter.apply(undefined, args); } - , trigger: function (eventName) { + , trigger: function (eventName, args) { var nextState = me.currentState(true).events[eventName]; - nextState && me.setState(nextState); + nextState && me.setState(nextState, args); } , _states: {}