diff --git a/templates/view.coffee.erb b/templates/view.coffee.erb index 83eff0f..79527b6 100644 --- a/templates/view.coffee.erb +++ b/templates/view.coffee.erb @@ -7,16 +7,18 @@ class window.<%= object_name %>View extends Backbone.View template: JST['<%= underscore_name %>s/view'] className: '<%= css_class_name %>' initialize: -> - @model.bind('change', @render) - @model.bind('remove', @remove) + _.bindAll(this, 'remove') + + @model.bind('change', this.render) + @model.bind('remove', this.remove) @model.view = this render: => - $(@el).html(@template(@model.toJSON())) + $(@el).html(this.template(@model.toJSON())) this.$('button.save').text(if @model.isNew() then 'Create' else 'Update') this.$('button.delete')[if @model.isNew() then 'hide' else 'show'](); this save: => - @model.save(@attributes()) + @model.save(this.attributes()) destroy: => if confirm("Are you sure?") @model.destroy diff --git a/templates/view_spec.coffee.erb b/templates/view_spec.coffee.erb index 1904641..67b8415 100644 --- a/templates/view_spec.coffee.erb +++ b/templates/view_spec.coffee.erb @@ -30,13 +30,13 @@ describe '<%= object_name %>View', -> $('#container').append(view.el) view.$('button.delete').trigger('click') - expect(model.destroy).toHaveBeenCalled - expect(window.confirm).toHaveBeenCalled + expect(model.destroy).toHaveBeenCalled() + expect(window.confirm).toHaveBeenCalled() it 'should remove the view when the model is destroyed', -> $('#container').append(view.render().el) - expect($('button.save')).toExist + expect($('button.save')).toExist() model.trigger('remove') - expect($('button.save')).not.toExist + expect($('button.save')).not.toExist()