2013-01-22 73 views
1

我正在構建我的第一個Backbone應用程序,其結構與Todo MVC example類似,具有Require.js並且還使用Backbone LocalStorage。問題是,當我在HomeView運行TweetsCollection.fetch(),螢火給我的錯誤:TypeError: options is undefinedvar method = options.update ? 'update' : 'reset';Backbone.js收集提取錯誤

TweetsCollection:

define([ 
    'underscore', 
    'backbone', 
    'backboneLocalStorage', 
    'models/TweetModel' 
], function(_, Backbone, Store, TweetModel) { 

'use strict'; 

    var TweetsCollection = Backbone.Collection.extend({ 

     model: TweetModel, 

     localStorage: new Store('tweets-storage'), 

     initialize: function() { 
      console.log('Collection init...'); 
     } 

    }); 

    return new TweetsCollection(); 

}); 

HomeView的init:

initialize: function() { 
      this.listenTo(TweetsCollection, 'add', this.addOne); 
      this.listenTo(TweetsCollection, 'reset', this.addAll); 
      this.listenTo(TweetsCollection, 'all', this.render); 

      TweetsCollection.fetch(); // <- Error here 
     }, 

我嘗試按照上面的例子,但我真的迷失了。

回答

0

發生錯誤的代碼行位於Backbone的success回調中,該回調由Backbone.sync執行。下面就是該方法看起來像骨幹0.9.10

options.success = function(collection, resp, options) { 
    var method = options.update ? 'update' : 'reset'; 
    collection[method](resp, options); 
    if (success) success(collection, resp, options); 
    }; 

此前0.9.10版本,骨幹回調簽名是:

options.success = function(resp, status, xhr) { ... 

Backbone.localStorage插件,你顯然使用,executes the callback method as follows (line 146)

if (options && options.success) 
    options.success(resp); 

正如您所看到的,它不會按正確的順序傳遞參數,並且缺少options a完全的參數,這是你看到錯誤的地方。

因此,似乎Backbone.localStorage插件目前與最新的Backbone版本不兼容。

編輯:我去報告這個問題給localStorage插件的作者,但看起來像已經有一個GitHub issue and pull request來解決這個問題。它尚未合併,所以在此期間,您可以使用phoey's fork或降級到Backbone 0.9.9

+0

謝謝!降級到0.9.9 – Potty