Skip to content

Commit

Permalink
enhanced Ki.State.plugin feature so that it can optionally be provide…
Browse files Browse the repository at this point in the history
…d hash objects that get added to a plugged-in state upon creation.
  • Loading branch information
mlcohen committed Mar 6, 2011
1 parent 6b35667 commit f410bde
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
21 changes: 18 additions & 3 deletions frameworks/foundation/system/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,8 @@ Ki.State = SC.Object.extend({

/**
Use this when you want to plug-in a state into a statechart. This is beneficial
in cases where you split your statechart's states up into multiple files.
in cases where you split your statechart's states up into multiple files and
don't want to fuss with the sc_require construct.
Example:
Expand All @@ -863,19 +864,33 @@ Ki.State = SC.Object.extend({
a: Ki.State.plugin('path.to.a.state.class'),
b: Ki.State.pluing('path.to.another.state.class)
b: Ki.State.plugin('path.to.another.state.class')
})
})
}}}
You can also supply hashes the plugin feature in order to enhance a state or
implement required functionality:
{{{
SomeMixin = { ... };
stateA: Ki.State.plugin('path.to.state', SomeMixin, { ... })
}}}
@param value {String} property path to a state class
@param args {Hash,...} Optional. Hash objects to be added to the created state
*/
Ki.State.plugin = function(value) {
var args = SC.A(arguments); args.shift();
var func = function() {
return SC.objectForPropertyPath(value);
var klass = SC.objectForPropertyPath(value);
return klass.extend.apply(klass, args);
};
func.statePlugin = YES;
return func;
Expand Down
68 changes: 68 additions & 0 deletions frameworks/foundation/tests/state/plugin/mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// ==========================================================================
// Ki.Statechart Unit Test
// ==========================================================================
/*globals Ki TestState */

TestState = null;
var obj, MixinA, MixinB, stateA, stateB, stateC;

module("Ki.State.plugin: Mixin Tests", {
setup: function() {

MixinA = {
isMixinA: YES
};

MixinB = {
isMixinB: YES
};

TestState = Ki.State.extend({
isTestState: YES
});

obj = SC.Object.create(Ki.StatechartManager, {

initialState: 'stateA',

stateA: Ki.State.plugin('TestState'),

stateB: Ki.State.plugin('TestState', MixinA),

stateC: Ki.State.plugin('TestState', MixinA, MixinB)

});

stateA = obj.getState('stateA');
stateB = obj.getState('stateB');
stateC = obj.getState('stateC');

},

teardown: function() {
obj = TestState = MixinA = MixinB = null;
stateA = stateB = stateC = null;
}

});

test("check plugin state A", function() {
ok(SC.kindOf(stateA, TestState));
ok(stateA.get('isTestState'));
ok(!stateA.get('isMixinA'));
ok(!stateA.get('isMixinB'));
});

test("check plugin state B", function() {
ok(SC.kindOf(stateB, TestState));
ok(stateB.get('isTestState'));
ok(stateB.get('isMixinA'));
ok(!stateB.get('isMixinB'));
});

test("check plugin state C", function() {
ok(SC.kindOf(stateC, TestState));
ok(stateC.get('isTestState'));
ok(stateC.get('isMixinA'));
ok(stateC.get('isMixinB'));
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var statechart = null;
externalState1 = null;
externalState2 = null;

module("Ki.Statechart: Plugin State Tests", {
module("Ki.State.plugin: Nest States Tests", {
setup: function() {

externalState1 = Ki.State.extend({
Expand Down

0 comments on commit f410bde

Please sign in to comment.