2012-01-26 39 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 :) –