view cleanup

This commit is contained in:
John Bintz 2011-05-13 10:06:44 -04:00
parent 755ffdb6c0
commit 8aea97569e
2 changed files with 10 additions and 8 deletions

View File

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

View File

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