Backbone.js - 集合未定义

时间:2012-07-19 11:31:49

标签: javascript backbone.js javascript-framework

我正在尝试学习backbone.js,但我仍然坚持抓取json并添加到集合中。集合未定义,我不知道为什么。 代码:

$(document).ready(function() {
    (function($) {
        //model
        window.Wine = Backbone.Model.extend();

        window.WineCollection = Backbone.Model.extend({
            url: "http://localhost/bootstrap/json.php",
            model: Wine
        });


        //views
        window.WineItemView = Backbone.View.extend({
            template: _.template($("#wine-item-template").html()),
            tagName: 'li',
            render: function() {
                $(this.el).html(this.template(this.model.toJSON()));
                return this;
            }
        });

        window.WineListView = Backbone.View.extend({
            tagName: 'ul',
            initialize: function() {
                this.model.bind('reset', this.render, this);

            },
            render: function() {

                _.each(this.model.models, function(wine) {
                    $(this.el).append(new WineItemView({
                        model: wine
                    }).render().el);
                }, this);
                return this;
            }
        });


        window.WineView = Backbone.View.extend({
            template: _.template($('#wine-template').html()),
            render: function() {
                $(this.el).html(this.template(this.model.toJSON()));
                return this;
            }
        });
        //Router
        window.AppRouter = Backbone.Router.extend({
            routes: {
                '': 'home',
                'wines/:id': "wineDetails"
            },
            home: function() {
                this.wineList = new WineCollection();

                this.wineListV = new WineListView({
                    model: this.wineList
                });
                this.wineList.fetch({success: function(){
                    console.log(this.wineList.models); // => 2 (collection have been populated)
                }});
                console.log(this.wineListV);

                $("#winelist").html(this.wineListV.render().el);
            },
            wineDetails: function(id) {
                this.wine = this.wineList.get(id);
                console.log(id);
                this.wineView = new WineView({
                    model: this.wine
                });
                $("#container").empty().html(this.wineView.render().el);
            }
        });


    })(jQuery);

    var app = new AppRouter();
    Backbone.history.start();
});

json.php返回:

[
   {
      "name":"Wine 1",
      "price":"100",
      "status":"available"
   },
   {
      "name":"Wine 1",
      "price":"100",
      "status":"available"
   },
   {
      "name":"Wine 1",
      "price":"100",
      "status":"available"
   }
]

我正在我的本地主机上测试它。

3 个答案:

答案 0 :(得分:5)

你犯了一个愚蠢的错字:

window.WineCollection = Backbone.Model.extend

应该是

window.WineCollection = Backbone.Collection.extend

请注意,按照惯例,您的WineListView类应该使用this.collection来引用WineCollection的实例,Backbone集合对象提供下划线迭代方法,而不是

.each(this.model.models, function(wine) {

DO

this.collection.each(function (wineModel) {

答案 1 :(得分:3)

你应该改变

window.WineCollection = Backbone.Model.extend({
            model: Wine,
            url: "wine.json"

        });

window.WineCollection = Backbone.Collection.extend({
            model: Wine,
            url: "wine.json"

        });

答案 2 :(得分:0)

欢迎来到SO :)这是你的第一个任务,所以让我让它变得难忘。当你说集合是undefined时,你可以为我们提供行号(或突出显示它),这样就可以很容易地进行调试。在initialize方法内,您必须将_.bindAll(this)置于您想要引用您想要的对象(默认为window对象)而不是下面的#1

 _.each(this.model.models, function (wine) {
     $(this.el).append(new WineItemView({
         model: wine
     }).render().el);
 }, this);

1

 this.model.each(function (wine) {
     $(this.el).append(new WineItemView({
         model: wine
     }).render().el);
 }, this);

试试这个

 window.WineListView = Backbone.View.extend({
     tagName: 'ul',
     initialize: function () {
         _.bindAll(this);
         this.model.on('reset', this.render);
     },
     render: function () {
         #1
     }
 });

万一你丢失了这里是代码http://jsbin.com/axixir/edit#javascript

的副本
相关问题