Created
December 20, 2013 00:30
-
-
Save polotek/8048650 to your computer and use it in GitHub Desktop.
Messing around with a pattern to update classes on a Backbone.Component.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $body = $('body') | |
, stateMap = { ON: 'On :)', OFF: 'Off :(', BLINK: 'Blinking!' } | |
, TriStateSwitch = Backbone.Component.extend({ | |
tagName: 'button', | |
events: { | |
'click': 'updateState' | |
}, | |
attrBindings: { | |
'alt': 'state' | |
} | |
initialize: function(opts) { | |
this.model = new Backbone.StateModel({ | |
states: ['OFF', 'ON', 'BLINK'], | |
values: stateMap, | |
currentState: opts.startState || 'OFF' }); | |
}, | |
render: function() { | |
this.$el.text('I have 3 states. Try it.'); | |
this.updateBindings(); | |
return this; | |
}, | |
updateState: function() { | |
this.model.nextState(); | |
} | |
}); | |
var b = new TriStateButton({}); | |
$body.html(b.render().$el); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $body = $('body') | |
, states = { ON: 'On :)', OFF: 'Off :(', BLINK: 'Blinking!' } | |
, TriStateSwitch = Backbone.Component.extend({ | |
tagName: 'button', | |
events: { | |
'click': 'updateState' | |
}, | |
initialize: function(opts) { | |
this.model = new Backbone.Model({ state: states[opts.state] || states.OFF }); | |
}, | |
render: function() { | |
this.$el.text('I have 3 states. Try it.'); | |
this.$el.attr('alt', this.model.get('state')); | |
return this; | |
}, | |
updateState: function() { | |
var switch = this.model.get('state'); | |
if (switch == states.OFF) { this.model.set('state', states.ON); } | |
else if(switch == states.ON) { this.model.set('state', states.BLINK); } | |
else if (switch == states.BLINK) { this.model.set('state', states.OFF); } | |
} | |
}); | |
var b = new TriStateButton({}); | |
$body.html(b.render().$el); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment