Created
November 12, 2012 05:50
-
-
Save CacheControl/4057719 to your computer and use it in GitHub Desktop.
Backbone view
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 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