diff --git a/templates/view.js.erb b/templates/view.js.erb index 9213b35..faa51c6 100644 --- a/templates/view.js.erb +++ b/templates/view.js.erb @@ -1,15 +1,17 @@ var <%= object_name %>View = Backbone.View.extend({ events: { 'click button.save': 'save', - 'click button.destroy': 'destroy' + 'click button.delete': 'destroy' }, attributeFields: [], template: JST['<%= underscore_name %>s/view'], className: '<%= underscore_name %>', initialize: function() { - _.bindAll(this, 'render', 'save', 'destroy'); + _.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())); @@ -17,7 +19,7 @@ var <%= object_name %>View = Backbone.View.extend({ return this; }, save: function() { - this.model.save(this.attributes()); + this.model.save(this.attributes(), { success: function(model) { model.view.render(); } }); }, destroy: function() { if (confirm("Are you sure?")) { diff --git a/templates/view_spec.js.erb b/templates/view_spec.js.erb index 59708d6..344d1af 100644 --- a/templates/view_spec.js.erb +++ b/templates/view_spec.js.erb @@ -18,10 +18,11 @@ describe('<%= object_name %>View', function() { beforeEach(function() { model = new <%= object_name %>({id: 1}); setFixtures('
'); + + view = new <%= object_name %>View({model: model}); }); it('should render with an update button', function() { - view = new <%= object_name %>View({model: model}); view.render(); expect(view.$('button.save')).toHaveText('Update'); @@ -31,7 +32,7 @@ describe('<%= object_name %>View', function() { spyOn(window, 'confirm').andReturn(true); spyOn(model, 'destroy'); - $('#facility').append(view.el); + $('#container').append(view.el); view.$('button.delete').trigger('click'); expect(model.destroy).toHaveBeenCalled();