繁体   English   中英

Backbone.js和localstorage

[英]Backbone.js & localstorage

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

骨架.js:570-未捕获的TypeError:对象不是函数

模型=新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"));

在您的Collection中,您将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")
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM