2016-02-18 48 views
1

我瘋了,我的問題!骨幹localStorage不使用好同步方法

我有一個模型,我想使用localStorage,但是當我在我的視圖中調用model.fetch時,出現錯誤「Error:A」url「屬性或函數必須指定」。

如果我不使用localStorage,這個錯誤是「正常的」,因爲我沒有定義url屬性。這裏的fetch調用應該使用Backbone.sync(也就是localStorage模塊的Backbone.localSync覆蓋吧?)。

但它永遠不會進入localStorage功能!就好像模塊從未加載過一樣。但我添加了一些console.log無處不在,backbone.localstorage被加載並正確初始化。

這就像骨幹被重新加載的地方,我失去了超越上Backbone.sync指向localStorage.localSync ...

錯誤發生在模塊/ config.js,渲染功能。

這裏是我的代碼:

型號/ configuration.js':

var Backbone = require('Backbone'); 
    Backbone.LocalStorage = require('Backbone.LocalStorage'); 

    // Exports the module 
    module.exports = Backbone.Model.extend({ 
     defaults:{ 
      id: 1 
      agencyName : 'xxx', 
      agencyId : 'xxx', 
      serviceUrl : 'xxx' 
     }, 

     localStorage: new Backbone.LocalStorage('Configuration') 
    }); 

數據/ configuration.js:

'use strict'; 

// Get class dependencies 
var Config = require('../models/configuration'); 

// export module 
module.exports = new Config(); 

控制器/ config.js:

'use strict'; 

// Gets the controller dependencies 
var region = require('../regions/main'), 
    dispatcher = require('../services/dispatcher'), 
    ConfigView = require('../modules/config'), 
    config = require('../data/configuration'); 

// manages the route for the home page 
module.exports = function() { 
    dispatcher.command('header:set', [ 'Configuration', false, true]); 
    region.show(new ConfigView({model: config})); 
}; 

modules/config.js:

'use strict'; 

// Adds the requires for the module 
var Backbone = require('Backbone'); 

// Exports the header module 
module.exports = Backbone.View.extend({ 

    // Sets the template 
    tpl: require('../templates/config.tpl'), 

    // Sets the class for the Module 
    className: 'home module', 

    // Sets the event for the menu module 
    events: { 
     'mouseup': 'onScreenTap' 
    }, 

    // Fired when the user clicks anywhere in the menu, to close it 
    onScreenTap: function(e) { 
     e.preventDefault(); 

     // if a menu item was clicked, it navigates to the module 
     var node = e.target; 
     if (node.classList.contains('btn')) { 
      this.model.save(); 
     } 
    }, 

    // Initializes the module 
    initialize: function() { 
    }, 

    // Renders the view 
    render: function() { 
     this.model.fetch(); 

     this.$el.html(this.tpl({ config: this.model })); 
     return this; 
    } 
}); 

回答

1

好吧,我發現了問題! 我到處都在使用「require(」Backbone「)」,但是在backbone.localStorage.js中,主幹需要「require(」backbone「)」! 因此,Backbone的2個實例被緩存,我沒有使用好的。3天的戰鬥,我很累!

希望這將有助於某人有一天..