[英]Backbone.js & localstorage
Trying to get my head around backbone.js. 试图让我的头在骨干.js。 This example is using Backbone Boilerplate and Backbone.localStorage and I'm coming up against a confusing problem; 这个例子使用了Backbone Boilerplate和Backbone.localStorage ,我遇到了一个令人困惑的问题。 When quizes.create(...) is called I get this error: 调用quizes.create(...)时出现此错误:
backbone.js:570 - Uncaught TypeError: object is not a function 骨架.js:570-未捕获的TypeError:对象不是函数
model = new this.model(attrs, {collection: this}); 模型=新this.model(attrs,{collection:this});
Quiz module code: 测验模块代码:
(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"));
In your Collection, you're naming model
as your Quiz
object, not the actual Quiz.Model
. 在您的Collection中,您将model
命名为Quiz
对象,而不是实际的Quiz.Model
。 So, when you call new this.model()
, you're actually calling Quiz()
- which is an object, not a function. 因此,当您调用new this.model()
,实际上是在调用Quiz()
-这是一个对象,而不是一个函数。 You need to change the code to: 您需要将代码更改为:
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.