backbone-generator/templates/view.js.erb

29 lines
723 B
Plaintext

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