diff --git a/templates/collection_view.js.erb b/templates/collection_view.js.erb index eef8e4b..f6b8c2f 100644 --- a/templates/collection_view.js.erb +++ b/templates/collection_view.js.erb @@ -4,7 +4,7 @@ var <%= plural_object_name %>View = Backbone.View.extend({ }, template: JST['<%= plural_underscore_name %>/list'], initialize: function() { - _.bindAll(this, 'render', 'addOne', 'addAll', 'new'); + _.bindAll(this, 'render', 'addOne', 'addAll', 'addNew'); this.collection.bind('refresh', this.addAll); @@ -23,6 +23,9 @@ var <%= plural_object_name %>View = Backbone.View.extend({ this.collection.each(this.addOne); }, addNew: function() { + var facility = new <%= object_name %>(); + this.collection.add(facility); + this.addOne(facility); } }); diff --git a/templates/collection_view_spec.js.erb b/templates/collection_view_spec.js.erb index 0958b07..b2ab2c7 100644 --- a/templates/collection_view_spec.js.erb +++ b/templates/collection_view_spec.js.erb @@ -14,6 +14,9 @@ describe('<%= plural_object_name %>View', function() { }); it('should add a new model when new is clicked', function() { + view.$('button.new').trigger('click'); + expect(view.$('.list')).toContain('.<%= underscore_name %>'); + expect(collection.length).toEqual(1); }); }); diff --git a/templates/view.js.erb b/templates/view.js.erb index 2febe89..9213b35 100644 --- a/templates/view.js.erb +++ b/templates/view.js.erb @@ -1,11 +1,13 @@ var <%= object_name %>View = Backbone.View.extend({ events: { - 'click button.save': 'save' + 'click button.save': 'save', + 'click button.destroy': 'destroy' }, attributeFields: [], template: JST['<%= underscore_name %>s/view'], + className: '<%= underscore_name %>', initialize: function() { - _.bindAll(this, 'render', 'save'); + _.bindAll(this, 'render', 'save', 'destroy'); this.model.bind('change', this.render); }, @@ -16,6 +18,11 @@ var <%= object_name %>View = Backbone.View.extend({ }, save: function() { this.model.save(this.attributes()); + }, + destroy: function() { + if (confirm("Are you sure?")) { + this.model.destroy(); + } } }); diff --git a/templates/view.jst.erb b/templates/view.jst.erb index 85ea5d7..eaaffac 100644 --- a/templates/view.jst.erb +++ b/templates/view.jst.erb @@ -1,2 +1,2 @@ - + diff --git a/templates/view_spec.js.erb b/templates/view_spec.js.erb index 2b403d8..c912d95 100644 --- a/templates/view_spec.js.erb +++ b/templates/view_spec.js.erb @@ -17,6 +17,7 @@ describe('<%= object_name %>View', function() { describe('existing record', function() { beforeEach(function() { model = new <%= object_name %>({id: 1}); + setFixtures('
'); }); it('should render with an update button', function() { @@ -25,5 +26,24 @@ describe('<%= object_name %>View', function() { expect(view.$('button.save')).toHaveText('Update'); }); + + it('should destroy the model' function() { + spyOn(window, 'confirm').andReturn(true); + spyOn(model, 'destroy'); + + $('#facility').append(view.el); + view.$('button.delete').trigger('click'); + + expect(model.destroy).toHaveBeenCalled(); + expect(window.confirm).toHaveBeenCalled(); + }); + + it('should remove the view when the model is destroyed', function() { + $('#container').append(view.render().el); + + expect($('button.save')).toExist(); + model.trigger('remove'); + expect($('button.save')).not.toExist(); + }); }); });