backbone-generator/templates/view.js.erb

32 lines
928 B
Plaintext

var <%= object_name %>View = Backbone.View.extend({
events: {
'click button.save': 'save',
'click button.delete': 'destroy'
},
attributeFields: [],
template: JST['<%= underscore_name %>s/view'],
className: '<%= underscore_name %>',
initialize: function() {
_.bindAll(this, 'render', 'save', 'destroy', 'remove');
this.model.bind('change', this.render);
this.model.bind('remove', this.remove);
this.model.view = this;
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
this.$('button.save').text(this.model.isNew() ? 'Create' : 'Update');
this.$('button.delete')[this.model.isNew() ? 'hide' : 'show']();
return this;
},
save: function() {
this.model.save(this.attributes(), { success: function(model) { model.view.render(); } });
},
destroy: function() {
if (confirm("Are you sure?")) {
this.model.destroy();
}
}
});