0

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

集合快照 在此处输入图像描述

//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() {}


});
4

1 回答 1

0

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

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

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

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

希望这可以帮助。

于 2013-04-07T17:59:27.427 回答