从db中删除模型后的backbone.js将其从视图中删除

时间:2013-03-01 12:40:59

标签: backbone.js view destroy

我遇到了问题 - 我为每个型号创建了一个删除链接&它从数据库中删除了...但我没有设法正确设置删除事件的回调,因为它是正确的可视化显示。

class Notes.Views.ArticlesIndex extends Backbone.View.     
  render: ->  
    $(@el).html(@template())  
    @collection.each(@appendEntry)  
    this  

  appendEntry: (article)->  
    view = new Notes.Views.Article(model: article)  
    $('ul#article_list').append(view.render().el)  


class Notes.Views.Article extends Backbone.View   
  template: JST['articles/menu']    
  events:  
    'click a.delete': 'deleteEntry'   
  initialize: ->  
    # putting @model.on('destroy',@render,this) didn't help    
  deleteEntry: (event)->  
    event.preventDefault()  
    @model.destroy()

更新!
作为@nEEbz答案的补充,添加@model.on('destroy',@remove,this)回调也有帮助!

1 个答案:

答案 0 :(得分:1)

试试这个:

deleteEntry: (event)->
    event.preventDefault()
    @model.destroy()
    @$el.remove()