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 @@ -