0

我正在尝试使用 Backbone 在 Rails 中构建应用程序。我面临一个问题,我在尝试阅读它的模型上得到未定义。

这是我的代码。

// 收藏

Quizzer.Collections.Posts = Backbone.Collection.extend({

   model: Quizzer.Models.Post,
   url: "/posts"

});

// 模型

Quizzer.Models.Post = Backbone.Model.extend({

});

//PostIndex 视图

Quizzer.Views.PostsIndex = Backbone.View.extend({

    template: JST['posts/index'],
    el: '#posts',

    render: function(){
        $(this.el).html(this.template);
        $(this.projectsCallView());
        return this;
    },

    projectsCallView: function(){
        var pp = new Quizzer.Views.Posts({ collection : new Quizzer.Collections.Posts });
        this.$("ul").append(pp.render().el)
    }

});

//帖子视图

Quizzer.Views.Posts = Backbone.View.extend({
    el: '#container',

    template: JST['posts/posts'],

    initialize: function(){
        this.listenTo(this.collection, 'reset', this.render);   
        this.collection.fetch({ reset:true });
        },

    render:function(){
        $(this.el).html(this.template());
        _.each(this.collection,this.addOne);
        return this;
    },

    addOne: function(model){
        console.log(model);
        var vv = new Quizzer.Views.PostW({ model: model })
        $("ul").append(vv.render().el)
    }

});

//PostW 视图

Quizzer.Views.PostW = Backbone.View.extend({
    template: JST['posts/postsw'],

    render: function() {
        console.log(this.model)
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    }
});

你能告诉我的问题在哪里吗?

4

1 回答 1

1

代替

_.each(this.collection,this.addOne);

this.collection.each(this.addOne);
于 2013-08-14T16:08:56.423 回答