backbone-generator/templates/view.js.erb

29 lines
723 B
Plaintext
Raw Normal View History

2011-04-15 18:55:37 +00:00
var <%= object_name %>View = Backbone.View.extend({
2011-05-03 19:43:19 +00:00
events: {
2011-05-03 19:53:36 +00:00
'click button.save': 'save',
'click button.destroy': 'destroy'
2011-05-03 19:43:19 +00:00
},
attributeFields: [],
template: JST['<%= underscore_name %>s/view'],
2011-05-03 19:53:36 +00:00
className: '<%= underscore_name %>',
2011-04-15 18:55:37 +00:00
initialize: function() {
2011-05-03 19:53:36 +00:00
_.bindAll(this, 'render', 'save', 'destroy');
2011-05-03 19:43:19 +00:00
this.model.bind('change', this.render);
2011-04-15 18:55:37 +00:00
},
render: function() {
2011-04-29 19:05:54 +00:00
$(this.el).html(this.template(this.model.toJSON()));
2011-05-03 19:43:19 +00:00
this.$('button.save').text(this.model.isNew() ? 'Create' : 'Update');
return this;
2011-05-03 19:43:19 +00:00
},
save: function() {
this.model.save(this.attributes());
2011-05-03 19:53:36 +00:00
},
destroy: function() {
if (confirm("Are you sure?")) {
this.model.destroy();
}
2011-04-15 18:55:37 +00:00
}
});