2013-10-29 159 views
0

是否可以在tagName中提供的標籤內添加另一個標籤?Backbone.js - 在li標籤內添加按鈕標籤

我有這樣的代碼:

BookView = Backbone.View.extend(
    tagName: "li" 
    events: 
    "click .delete": "deleteBook" 

    deleteBook: (ev) -> 

    #delete operation /books/id 
    @book.destroy success: -> 
     router.navigate "", 
     trigger: true 

而且我希望把未來每li標記名內的按鈕,所以當有人點擊它,它會刪除該記錄。我怎麼能這樣做?

例如:

<ul> 
<li>Some text <button type="submit">Delete</button></li> 
</ul> 

回答

0

你只添加一個<button type="button" class="delete">(不type="submit",這隻會帶來麻煩,讓你做更多的工作)到您的模板:

render: -> 
    @$el.append('Some text <button type="button" class="delete">Delete</button>') 
    @ 

然後綁定到'destroy'來自模型的視圖中的事件initialize

initialize: -> 
    @listenTo(@model, 'destroy', @remove_and_redirect) 

那麼你deleteBook將破壞模型:

deleteBook: -> @model.destroy() 

和你remove_and_redirect將清理和導航:

remove_and_redirect: -> 
    @remove() 
    router.navigate('', trigger: true) 

簡化的演示:http://jsfiddle.net/ambiguous/X3SQa/