是否可以在tagName
?
我有这段代码:
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 :(得分: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)
答案 1 :(得分:0)
使用包含按钮的模板。