Backbone.js Collections视图forEach不';行不通

Backbone.js Collections视图forEach不';行不通,backbone.js,backbone.js-collections,Backbone.js,Backbone.js Collections,我试图使用集合来列出来自api的数据。 但问题是,当我使用forEach时,我调用的函数(addOne)没有运行 还有一点我怀疑是做错了。我的集合是否应该将返回的JSON保存在这样的模型下 Object -> models -> 0 -> attributes -> ... 我的看法是: s.Views.Fs = Backbone.View.extend({ className: "", template: _.template("<%= name

我试图使用集合来列出来自api的数据。 但问题是,当我使用forEach时,我调用的函数(addOne)没有运行

还有一点我怀疑是做错了。我的集合是否应该将返回的JSON保存在这样的模型下

Object -> models -> 0 -> attributes -> ...
我的看法是:

s.Views.Fs = Backbone.View.extend({
    className: "",
    template: _.template("<%= name %>"),
    initialize: function() {

    },
    render: function() {
        this.collection.forEach(this.addOne, this);
    },
    addOne: function(f) {
        alert("a");
        var fV = new s.Views.PF({model: f});
        this.$el.append(fV.render().el);
    }
});
我的模型:

s.Models.F = Backbone.Model.extend( {
    urlRoot: 'api/fs/',
    defaults: {
        ...
    },
    ...
    parse: function(response) {
            return response;
    },
});
我的路线(和应用程序):


侦听事件是由
this.collection.on('add',this.addOne,this')进行的在集合的视图下。下面是测试代码的摘要(感谢提示“mu太短”):

查看

s.Views.Fs = Backbone.View.extend({
    className: "",
    template: _.template("<%= name %>"),
    initialize: function() {
        this.collection.on('add', this.addOne, this);
        this.collection.on('reset', this.render, this);
    },
    render: function() {
        this.collection.forEach(this.addOne, this);
    },
    addOne: function(f) {
        var fV = new s.Views.PF({model: f});
        fV.render();
        this.$el.append(fV.el);
    }
});
型号

s.Models.F = Backbone.Model.extend( {
    urlRoot: 'api/fs/',
    // No need to parse here.
});
路由器

var sApp = new (Backbone.Router.extend({
    f_a: function() {
        this.fL= new s.Collections.FL();
        this.fLV= new s.Views.Fs({collection: this.fL});
        this.fLV.render();
        $("#content").html(this.fLV.el);
        this.fL.fetch();
    },
});

fetch
是一个AJAX调用,您必须侦听事件或添加一个
success
处理程序才能知道它何时获得一些数据。
s.Collections.FL = Backbone.Collection.extend({
    url: "api/fs/",
    model: s.Models.F,
});
s.Models.F = Backbone.Model.extend( {
    urlRoot: 'api/fs/',
    // No need to parse here.
});
var sApp = new (Backbone.Router.extend({
    f_a: function() {
        this.fL= new s.Collections.FL();
        this.fLV= new s.Views.Fs({collection: this.fL});
        this.fLV.render();
        $("#content").html(this.fLV.el);
        this.fL.fetch();
    },
});