backbone-generator/templates/view.js.erb

32 lines
928 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',
2011-05-03 20:10:26 +00:00
'click button.delete': '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 20:10:26 +00:00
_.bindAll(this, 'render', 'save', 'destroy', 'remove');
2011-05-03 19:43:19 +00:00
this.model.bind('change', this.render);
2011-05-03 20:10:26 +00:00
this.model.bind('remove', this.remove);
this.model.view = this;
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');
2011-05-10 01:47:00 +00:00
this.$('button.delete')[this.model.isNew() ? 'hide' : 'show']();
return this;
2011-05-03 19:43:19 +00:00
},
save: function() {
2011-05-03 20:10:26 +00:00
this.model.save(this.attributes(), { success: function(model) { model.view.render(); } });
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
}
});