繁体   English   中英

主干集合以查看到模板

[英]backbone collection to view to template

我正在连接到第三方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

如果可以将每个数组元素放入自己的模型中会很好,但是我不确定如何做到这一点。

谢谢!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM