Created
July 22, 2015 18:58
-
-
Save maxparm/71c60fc9ca1f5222a550 to your computer and use it in GitHub Desktop.
Backbone View State
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
<div class="js-ui-content"></div> | |
<script id="my-template" type="text/html"> | |
<% if (!data.state.hasConfirmed) { %> | |
Display name? <button class="js-ui-confirm">Yes</button> | |
<% } else { %> | |
Hello <%= data.model.name %>! | |
<% } %> | |
</script> |
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 MyView = Marionette.ItemView.extend({ | |
el: $('.js-ui-content'), | |
template: '#my-template', | |
events: { | |
'click .js-ui-confirm': 'setHasConfirmedToTrue' | |
}, | |
initialize: function () { | |
this.state = new Backbone.Model({hasConfirmed: false}); | |
this.listenTo(this.state, "change", this.render); | |
this.listenTo(this.model, "change", this.render); | |
}, | |
serializeData: function () { | |
return { | |
variable: 'data', | |
data: { | |
model: this.model.toJSON(), | |
state: this.state.toJSON() | |
} | |
}; | |
}, | |
setHasConfirmedToTrue: function (e) { | |
e.preventDefault(); | |
this.state.set({hasConfirmed: true}) | |
} | |
}); | |
view = new MyView({model: new Backbone.Model({name: 'World'})}); | |
view.render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment