试图让我的头在骨干.js。这个例子使用了Backbone Boilerplate和Backbone.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"));
最佳答案
在您的收藏夹中,您将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")
});
关于javascript - Backbone.js和localstorage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9020952/