1

在这段代码...

_.each(this.photos, function(element,index,list) {
    console.log('element...');
    console.log(element);
    var photoView = new PhotoView({photo:element});
    self.$el.append(photoView.render());
});

element是整个 this.photos 集合。为什么集合中不只有 10 个照片元素中的一个?

编辑:这是我填充照片集的方法....

loadPhotos: function(memberId) {
    var self = this;
    this.photos = new PhotosCollection([]); 
    this.photos.on('error', this.eventSyncError, this);
    this.photos.fetch({
        url: this.photos.urlByMember + memberId, 
        success: function(collection,response,options) {
            console.log('Fetch photos success!');
            self.render();
        }
    });
},

该系列可以很好地加载模型。在 Chrome 控制台中,我可以看到模型的集合。我不确定出了什么问题。我无法使用下面海报推荐的任何方法来迭代集合。

4

3 回答 3

3

您使用的_.each方法不正确。下划线方法需要直接在集合上调用:

this.photos.each(function(element,index,list) {
    console.log('element...');
    console.log(element);
    var photoView = new PhotoView({photo:element});
    self.$el.append(photoView.render()); 
});

或者如果你想使用_.eachfrom 你需要传入models属性而不是集合对象本身作为列表:

_.each(this.photos.models, function(element,index,list) {
    console.log('element...');
    console.log(element);
    var photoView = new PhotoView({photo:element});
    self.$el.append(photoView.render());
});
于 2013-08-17T21:03:47.600 回答
0

应该使用this.photos.each(function(elt, index, list){...}),而不是_.each(this.photos,...)因为this.photos不是 underscorejs_.chain对象。

于 2013-08-17T21:05:33.983 回答
0

谢谢你的建议!如果没有你上面的所有建议,我永远不会想到这一点。所以问题来了……

在父视图中,这会加载特定成员的照片记录...

loadPhotos: function(memberId) {
    var self = this;
    this.photos = new PhotosCollection([]); 
    this.photos.on('error',this.eventSyncError,this);
    this.photos.fetch({
        url: this.photos.urlByMember + memberId, 
        success: function(collection,response,options) {
            self.render();
        } 
    });
},

仍然在父视图中,Backbone.Subviews 在渲染时使用它来调用每个子视图。请注意我如何将 this.photos 传递给 subvw-photos ......

subviewCreators: {
    "subvw-profile": function() {
        var options = {member: this.member};
        // do any logic required to create initialization options, etc.,
        // then instantiate and return new subview object
        return new ProfileView( options );
    },
    "subvw-photos": function() {
        var options = {photos: this.photos};
        return new PhotosView( options );
    },
    "subvw-comments": function() {
        var options = {};
        return new CommentsView( options );
    }
},

这是在 subvw-photos 子视图中。请注意 intialize 如何接受集合作为参数。看到这个问题了吗?...

initialize: function(photos) {
    Backbone.Courier.add(this);
    this.photos = photos;
},

render: function() {
    console.log('rendering photosview now...');
    var self = this;
    this.photos.each(function(element,index,list) {
        var photoView = new PhotoView({photo:element});
        $(self.el).append(photoView.render());
    });
    return this;
},

我正在传递一个包含照片集的对象以进行初始化,但随后将其视为只是照片集的参考。我不得不将 subvw-photos 的初始化更改为以下...

initialize: function(args) {
    Backbone.Courier.add(this);
    this.photos = args.photos;
},

然后当然所有其他代码都神奇地开始工作了:-/

再次感谢您的提示!你绝对让我走上正轨:-)

于 2013-08-18T13:56:56.940 回答