Backbone.js - 在li标签内添加按钮标签

时间:2013-10-29 11:42:38

标签: ruby-on-rails events backbone.js coffeescript

是否可以在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>

2 个答案:

答案 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)

简化演示:http://jsfiddle.net/ambiguous/X3SQa/

答案 1 :(得分:0)

使用包含按钮的模板。