Skip to content

Instantly share code, notes, and snippets.

@maxparm
Created July 22, 2015 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxparm/71c60fc9ca1f5222a550 to your computer and use it in GitHub Desktop.
Save maxparm/71c60fc9ca1f5222a550 to your computer and use it in GitHub Desktop.
Backbone View State
<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>
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