0

是否可以在提供的标签中添加另一个标签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>
4

2 回答 2

0

您只需在模板中添加一个<button type="button" class="delete">(not 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/

于 2013-10-29T16:31:47.713 回答
0

使用包含您的按钮的模板。

于 2013-10-29T16:33:30.643 回答