Skip to content

Instantly share code, notes, and snippets.

@CacheControl
Created November 12, 2012 05:50
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 CacheControl/4057719 to your computer and use it in GitHub Desktop.
Save CacheControl/4057719 to your computer and use it in GitHub Desktop.
Backbone view
var MovieView = Backbone.View.extend({
listTemplate: _.template($("#movieTemplate").html()),
editTemplate: _.template($("#movieEditTemplate").html()),
initialize: function() {
this.model.on('change', this.render, this);
this.model.on('destroy', this.remove, this);
this.model.on('error', this.invalid, this);
},
render: function() {
this.$el.html(this.listTemplate(this.model.toJSON()));
return this;
},
events: {
'dblclick span.title': 'editSelf',
'click a.delete': 'destroySelf',
'click span.favorite': 'toggleFavorite',
'click span.non-favorite': 'toggleFavorite',
'click a.btn-primary': 'saveButton',
'click a.cancel': 'cancelButton',
'keypress input[type=text].title': 'saveEnter'
},
editSelf: function() {
this.$el.html(this.editTemplate(this.model.toJSON()));
this.$('input.title').focus();
return this;
},
toggleFavorite: function() {
this.model.toggleFavorite();
},
invalid: function() {
$('input.title').css('background-color', 'red');
},
saveEnter: function(e) {
if(e.keyCode == 13) {
this.saveButton();
}
},
saveButton: function() {
this.model.set({title: this.$('input.title').val()});
this.render();
},
cancelButton: function() {
this.render();
},
destroySelf: function() {
this.model.destroy,
this.remove()
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment