javascript - 主干 JS 模板仅打印集合中的最后一项

标签 javascript backbone.js

我的主干 View 有问题。我正在尝试渲染一组日期对象,并且每个日期对象都包含另一个时间对象数组。我从我的 API 中获得了很好的集合,但是一旦它被渲染,它就会显示:

enter image description here

从我的控制台上可以看到,这基本上是我收藏的最后一项。 API 调用实际返回有效输出。

这是我的模板的样子:

<table id="stbl" class="table table-striped table-bordered">
     <% _.each(slots, function(slot) { %>
              <tr>
                <td>
                   <strong> <%- slot.startDate %> </strong>
                </td>
                <% _.each(slot.timeSlots, function(t) { %>
                    <td>
                       <button id="le-time" class="btn btn-primary"><%- t %></button>
                    </td>
                <% }); %>
               </tr>
         <% }); %>
</table>

这是我的收藏:

kronos.Slots = Backbone.Collection.extend({
    model: kronos.Slot,
    url: '/api/freeslots'
});

这就是我的观点:

kronos.TableView = Backbone.View.extend({
    initialize: function () {
        var self = this;
        this.collection.fetch({
            success: function (collection, response) {
                _.each(response, function (i) {
                    console.log(i);
                });
                self.render();
            },
            data: $.param({ orgID: 4 })
        });
        this.collection.on('reset', this.render, this);
    },
    template: _.template($('#slotsTable').html()),
    render: function () {
        $('#slotsContainer').html(this.template({
            slots: this.collection.toJSON()
        }));
    }
});

fetch 调用返回的 JSON 数据

[
    {"id":0, "startDate":"04/11/2013", "serviceID":241, "providerID":223, "timeSlots": ["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"]},
    {"id":0, "startDate":"05/11/2013", "serviceID":241, "providerID":223, "timeSlots": ["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"]}
]

你能帮我理解我做错了什么吗?

谢谢

最佳答案

在集合中获取数据时,Backbone 将执行 collection.set与数据:

set collection.set(models, [options])
The set method performs a "smart" update of the collection with the passed list of models. If a model in the list isn't yet in the collection it will be added; if the model is already in the collection its attributes will be merged; and if the collection contains any models that aren't present in the list, they'll be removed.

你所有的模型都有相同的 id,0,这似乎无可救药地混淆了 Backbone:看这个演示 http://jsfiddle.net/T3fmx/

要解决您的难题,您可以:

关于javascript - 主干 JS 模板仅打印集合中的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754293/

相关文章:

javascript - SVG 上的 .mouseover() 事件表现得很奇怪

backbone.js - 如何识别未定义的内容

javascript - typescript 错误 : A 'super' call must be the first statement in the constructor when a class contains initialized properties

javascript - 谷歌地图 : Add a marker to the embedded Google map without their API

jquery - 即使出现服务错误,主干模型也会被破坏

node.js - 如何在backbonejs中集成Stripe "Pay with Card"

javascript - Backbone.js:为什么这个事件没有绑定(bind)?

javascript - 如何在 Javascript 中比较一个数组的元素与另一个数组的索引?

javascript - 浏览器超时 : the server at example. com 响应时间太长

javascript - 如何从请求nodejs获取ip