2012-01-26 33 views
0

試着讓我的腦袋繞過backbone.js。這個例子是使用Backbone BoilerplateBackbone.localStorage,我想出了一個令人困惑的問題;當quizes.create(...)被調用我得到這個錯誤:Backbone.js&localstorage

backbone.js:570 - Uncaught TypeError: object is not a function

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")); 

回答

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") 
}); 
+0

omg謝謝! +10000 :) –