无法循环通过主干集合

时间:2013-04-07 16:17:56

标签: jquery backbone.js layout-manager

我正在使用带有主干的布局管理器,我无法从集合中获取模型。 下面是代码。我无法从this.collection.models获取元素,即使元素存在,它也显示为空。

收集snopshot enter image description here

//Models
App.Models.StoreModel = Backbone.Model.extend({});

//Featured Products Collections
App.Collections.StoreFeaturedProducts = Backbone.Collection.extend({  

    model : App.Models.StoreModel,
    url:"../JSON/products.json",
    parse : function(response){
        return response.products;  
    }

});


//In Router 
this.featureList = new App.Collections.StoreFeaturedProducts();     /* initialize featureList */
this.featureView = new App.Views.DisplayView2({collection : this.featureList}); /* create featureSlider View object */
App.Layouts.AppLayout.insertViews({     /* insert view to layout */
            "wrapper2" : this.featureView
});

this.featureList.fetch();   /* fetch json */

//Render Layout
App.Layouts.AppLayout.render();

//View![enter image description here][2]
App.Views.DisplayView2 = Backbone.View.extend({

    template : '#view1',

    initialize : function(options) {
        this.collection.bind('reset', this.render, this);
        //this.model.bind('reset', this.render, this);
    },


    beforeRender : function() {
            console.log(this.collection) - shows the elements
        this.collection.each(function(m) {
        //does not go through
    }, this);
    }

    serialize : function() {}


});

1 个答案:

答案 0 :(得分:0)

如果你真的看到你写的行上的元素:

console.log(this.collection) - shows the elements

应该通过集合'models属性访问该特定数组:

_.each(this.collection.models, function(m) {
    // ...
});

希望这有帮助。