【问题标题】:backbone collection to view to template要查看到模板的主干集合
【发布时间】:2015-09-15 20:08:19
【问题描述】:

我正在连接到一个第 3 方 API,该 API 返回一个包含数组的对象。

我正在尝试将其放入主干集合中,然后通过管道将其输出到视图中。

我尝试了很多东西,最近的一个很简单,像这样:

       var MyCollection = Backbone.Collection.extend({
           url: '/api/data',
           parse: function (resp) {
             return JSON.parse(resp);
           },
       });

       var myCollection = new MyCollection();
       myCollection.fetch();

       return Backbone.View.extend({
           template: _.template(tmpl),

       render: function() {           
           this.$el.html(this.template({
                coll: myCollection.toJSON()
            }));
            return this;
       }

这只是在我的模板中给我 [Object Object]。

如果我把它写到控制台,我只会看到:

YourCollection

[Object]
     yourdata.metadata: "www.xyz.edu/"
     value: Array[3]
        0: Object
           Id: "000"
           Name: "Name0"
           IsValid: True
        1: Object
           ID: "111"
           Name: "name1"
           IsValid: True
        3: Object
           ID: "222"
           Name: "name2"
           IsValid: True

如果我可以将每个数组元素放入它自己的模型中,那就太好了,但我不知道该怎么做。

谢谢!

【问题讨论】:

    标签: javascript backbone.js backbone-views


    【解决方案1】:

    似乎您需要在 parse 方法中过滤实际集合:

    function (resp) {
      return JSON.parse(resp).value;
    }
    

    【讨论】:

    • 但我确实有一个解析方法。它在我的收藏中
    • 在末尾备注.value
    • 这让我更接近了!这摆脱了容器对象,只剩下数组。
    猜你喜欢
    • 1970-01-01
    • 2015-05-30
    • 2015-08-31
    • 2015-09-05
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多