I am trying to explore Backbone.StateManager but couldn’t find much material on this.
I have gone through the documentation but there is no simple example of using it.
I have made an example like this
(function($) {
var UserInputView = Backbone.View.extend({
states: {
foo: {
enter: function () {
alert('hi');
return console.log('enter bar');
},
exit: function () {
alert('hi');
return console.log('exit foo');
},
transitions: {
transitions: {
'onBeforeExitTo:anotherState': function () {
alert('hi');
},
'onExitTo:anotherState': function () {
alert('hi');
},
'onBeforeEnterFrom:anotherState': function () {
alert('hi');
},
'onEnterFrom:anotherState': function () {
alert('hi');
}
}
}
},
bar: {
enter: function () {
alert('hi');
return console.log('enter bar');
},
exit: function () {
alert('hi');
return console.log('exit bar');
},
}
},
initialize: function () {
var statemanager;
alert('intialized');
console.log(this.states);
statemanager = Backbone.StateManager.addStateManager(this.states);
return statemanager;
},
render: function () {
alert('render');
}
});
var user = new UserInputView();
})(jQuery);
In all this code only intialization function is working. Rest of the code is not functioning. Please guide
Basically, your code misses a state change and a correct target for
Backbone.StateManager.addStateManager. As stated in the documentation, to use Backbone.StateManager with objectswhich means that the target for
Backbone.StateManager.addStateManagershould be your object instance and that you can start changing state withmodel.triggerState. For example :A demo based on your code, to visualize some state manipulations