Backbone.js& localStorage的

时间:2012-01-26 15:56:20

标签: javascript backbone.js local-storage

试图绕过backbone.js。这个例子正在使用Backbone BoilerplateBackbone.localStorage,我遇到了一个令人困惑的问题;调用quizes.create(...)时出现此错误:

  

backbone.js:570 - 未捕获TypeError:对象不是函数

     

model = new this.model(attrs,{collection:this});

测验模块代码:

(function(Quiz) {
Quiz.Model = Backbone.Model.extend({ /* ... */ });

Quiz.Collection = Backbone.Collection.extend({
    model: Quiz,
    localStorage: new Store("quizes")
});
quizes = new Quiz.Collection;

Quiz.Router = Backbone.Router.extend({ /* ... */ });

Quiz.Views.Question = Backbone.View.extend({
    template: "app/templates/quiz.html",

    events: {
        'click #save': 'saveForm'
    },

    initialize: function(){
        _.bindAll(this);
        this.counter = 0;
    },

    render: function(done) {
        var view = this;
        namespace.fetchTemplate(this.template, function(tmpl) {
            view.el.innerHTML = tmpl();
            done(view.el);
        });
    },
    saveForm: function(data){
        if (this.counter <= 0) {
            $('#saved ul').html('');
        }
        this.counter++;
        var titleField = $('#title').val();
        console.log(quizes);
        quizes.create({title: titleField});

    }

});

})(namespace.module("quiz"));

1 个答案:

答案 0 :(得分:3)

在您的收藏集中,您将model命名为Quiz个对象,而不是实际的Quiz.Model。因此,当您调用new this.model()时,您实际上正在调用Quiz() - 这是一个对象,而不是一个函数。您需要将代码更改为:

Quiz.Collection = Backbone.Collection.extend({
  model: Quiz.Model, // Change this to the actual model instance
  localStorage: new Store("quizes")
});