[英]Backbone.js & localstorage
試圖讓我的頭在骨干.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"));
在您的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.