diff --git a/readme.md b/readme.md index f6625e8..fc1664c 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/storyboard.js b/src/storyboard.js index 138bacb..cf0d858 100644 --- a/src/storyboard.js +++ b/src/storyboard.js @@ -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; }; diff --git a/test/unit/base.js b/test/unit/base.js index 3469652..6ce11fd 100644 --- a/test/unit/base.js +++ b/test/unit/base.js @@ -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() { @@ -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() { @@ -99,7 +130,6 @@ test("Cloning deeply", function() { this.parent.helper(); } }), - b : { enter : function() { this.counter = 0; @@ -113,10 +143,8 @@ test("Cloning deeply", function() { this.parent.helper(); } }, - ending : {} }, - helper : function() { this.counter += 10; }