Skip to content

Commit

Permalink
Issue #12 - adding simple function-only scene support.
Browse files Browse the repository at this point in the history
  • Loading branch information
iros committed May 10, 2013
1 parent dade432 commit f8bc214
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 5 deletions.
60 changes: 60 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,66 @@ var app = new Miso.Scene({
});
```

## Simplified Storyboards ##

While the main goal of Storyboard is to help you manage your transition enter and exit phases, you can create simplified storyboards that only have `enter` behaviour like so:

```javscript
var walkthrough = new Miso.Scene({
initial : 'one',
scenes : {
one : function() {
// do something
console.log("Doing task one.");
},
two : function() {
// do something else
console.log("Doing task two.");
}
three : {}
}
});
walkthrough.start().then(function() {
walkthrough.to("two");
});
// output:
// Doing task one.
// Doing task two.
```

The above is functionaly equivalent to:

```javscript
var walkthrough = new Miso.Scene({
initial : 'one',
scenes : {
one : {
enter : function() {
// do something
console.log("Doing task one.");
}
},
two : {
enter : function() {
// do something else
console.log("Doing task two.");
}
},
three : {}
}
});
walkthrough.start().then(function() {
walkthrough.to("two");
});
// output:
// Doing task one.
// Doing task two.
```

## Contributing ##

To build Miso.Storyboard you'll need npm, node.js's package management system and grunt
Expand Down
10 changes: 10 additions & 0 deletions src/storyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
// If there are scenes defined, initialize them.
if (options.scenes) {

// if the scenes are actually just set to a function, change them
// to an enter property
_.each(options.scenes, function(scene, name) {
if (typeof scene === "function") {
options.scenes[name] = {
enter : scene
};
}
});

// make sure enter/exit are defined as passthroughs if not present.
_.each(Storyboard.HANDLERS, function(action) {
options.scenes[action] = options.scenes[action] || function() { return true; };
Expand Down
38 changes: 33 additions & 5 deletions test/unit/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,40 @@ module("base", {
}
});

test("Function only scenes", function() {
var nums = [];
var sb = new Miso.Storyboard({
initial : "a",
scenes: {
a : function() {
nums.push(1);
},
b : function() {
nums.push(2);
},
c : function() {
nums.push(3);
},
d : {
enter : function() {
nums.push(4);
}
}
}
});

sb.start().then(function() {
sb.to("b").then(function() {
sb.to("c").then(function() {
sb.to("d").then(function() {
ok(_.isEqual(nums, [1,2,3,4]), "nums are equal");
});
});
});
});
});

test("Create storyboard", 3, function() {

app.start().then(function() {
app.to("b").then(function() {
app.to("ending").then(function() {
Expand All @@ -61,7 +93,6 @@ test("Create storyboard", 3, function() {
});

test("Cloning", 6, function() {

app.start().then(function() {
app.to("b").then(function() {
app.to("ending").then(function() {
Expand Down Expand Up @@ -99,7 +130,6 @@ test("Cloning deeply", function() {
this.parent.helper();
}
}),

b : {
enter : function() {
this.counter = 0;
Expand All @@ -113,10 +143,8 @@ test("Cloning deeply", function() {
this.parent.helper();
}
},

ending : {}
},

helper : function() {
this.counter += 10;
}
Expand Down

0 comments on commit f8bc214

Please sign in to comment.