backbone-generator/templates/collection_view.js.erb

32 lines
777 B
Plaintext

var <%= plural_object_name %>View = Backbone.View.extend({
events: {
'click button.new': 'addNew'
},
template: JST['<%= plural_underscore_name %>/list'],
initialize: function() {
_.bindAll(this, 'render', 'addOne', 'addAll', 'addNew');
this.collection.bind('refresh', this.addAll);
this.render();
this.collection.fetch()
},
render: function() {
$(this.el).html(this.template());
return this;
},
addOne: function(model) {
var view = new <%= object_name %>View({model: model});
this.$('.list').append(view.render().el);
},
addAll: function() {
this.collection.each(this.addOne);
},
addNew: function() {
var object = new <%= object_name %>();
this.collection.add(object);
this.addOne(object);
}
});