【问题标题】:Backbone fetch is adding new rows to view rather than updating the existingBackbone fetch 是添加新的行来查看而不是更新现有的
【发布时间】:2012-04-27 13:43:30
【问题描述】:

我仍在学习 Backbone,但我的理解是它应该在这种情况下自动处理更新视图。我的主索引视图是一个表,其中每一行都是单个模型的视图。

索引视图:

Tracker.Views.Friends ||= {}

class Tracker.Views.Friends.IndexView extends Backbone.View
  template: JST["backbone/templates/friends/index"]

  initialize: () ->
    _.bindAll(this, 'addOne', 'addAll', 'render');

    @options.friends.bind('reset', this.addAll);

  addAll: () ->
    @options.friends.each(this.addOne)

  addOne: (chaser) ->
    view = new Tracker.Views.Friends.FriendView({model : friend})
    this.$("tbody").append(view.render().el)

  render: ->
    $(this.el).html(this.template(friends: this.options.friends.toJSON() ))
    @addAll()

    return this

模型和集合:

class Tracker.Models.Friend extends Backbone.Model
  paramRoot: 'friend'

  defaults:
    name: null
    status: null

class Tracker.Collections.FriendsCollection extends Backbone.Collection
  model: Tracker.Models.Friend
  url: '/friends.json'

好友浏览:

Tracker.Views.Friends ||= {}

class Tracker.Views.Friends.FriendView extends Backbone.View
  template: JST["backbone/templates/friends/friend"]

  events:
    "click .destroy" : "destroy"

  tagName: "tr"

  destroy: () ->
    @options.model.destroy()
    this.remove()

    return false

  render: ->
    $(this.el).html(this.template(this.options.model.toJSON() ))    
    return this

friend.jst.ejs:

<td><a href="javascript:void(0);" data-friendid="<%= id %>" class="friend-link"><%= name %></a></td>
<td><span class="label"><%= status %></span></td>

index.jst.ejs:

<table id="friends_table" class="table table-striped table-bordered">
    <tr>
      <td>Name</td>
      <td>Status</td>
    </tr>
</table>

我最初使用重置实例化并填充集合,如下所示:

friends = new Tracker.Collections.FriendsCollection()
friends.reset data

然后我实例化我的索引视图并将我的集合传递给它:

view = new Tracker.Views.Friends.IndexView(friends: friends)

这一切都很好,并显示了一个表格,其中包含来自网络服务器的行。但是我想通过服务器上发生的更改定期更新好友列表,因此我使用 collection.fetch 方法如下(其中 updateStatus 与到目前为止描述的代码完全无关):

window.setInterval (->
  friends.fetch success: updateStatus
), 10000

数据从 fetch 返回并正确解析,但是它将行附加到我的表中,而不是更新现有行。我怎样才能按照我的预期进行这项工作?

【问题讨论】:

    标签: javascript backbone.js coffeescript


    【解决方案1】:

    当它被重置时,你永远不会真正清除表。

    更新您的addAll 函数以清除表格。像这样的:

    class Tracker.Views.Friends.IndexView extends Backbone.View
      template: JST["backbone/templates/friends/index"]
    
      # ...
    
      addAll: () ->
        @$("tbody").empty()
        @options.friends.each(this.addOne)
    
      # ...
    

    请注意,根据您的代码/交互的复杂程度,清除这种方式可能会有点泄漏。您可能需要在添加每个子视图时保存对它们的引用,然后在清除每个子视图时循环并调用您的自定义删除代码(如果有的话)。

    您可能还需要将表头包装在 index.jst.ejs 文件中,这样它就不会与表体的其余部分一起被清除:

    <table id="friends_table" class="table table-striped table-bordered">
        <thead>
            <tr>
              <td>Name</td>
              <td>Status</td>
            </tr>
        </thead>
    </table>
    

    【讨论】:

    • 第一部分有效,谢谢,但是如果我添加了 thead 标签,则不会添加任何行,它仍然只是一个带有标题的空表。
    • 还必须声明一个空的 tbody 标签。
    • 啊忘记在里面添加了。很好的收获。
    猜你喜欢
    • 2019-10-30
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多