Backbone localStorage不使用良好的同步方法

时间:2016-02-18 13:40:36

标签: javascript node.js backbone.js local-storage commonjs

我对我的问题发疯了!

我有一个模型,我想使用localStorage,但是当我在我的视图中调用model.fetch时,我收到错误"错误:A" url"必须指定属性或功能"。

此错误是"正常"如果我没有使用localStorage,因为我没有定义url属性。这里的fetch调用应该是使用Backbone.sync(也就是localStorage模块的Backbone.localSync覆盖吗?)。

但它永远不会进入localStorage功能!好像模块从未加载过。但我在所有地方添加了一些console.log,并加载了backbone.localstorage并正确初始化。

像Backbone一样在某处重新加载,我松开了Backbone.sync指向localStorage.localSync的覆盖...

在modules / config.js,render function中发生错误。

这是我的代码:

模型/ 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')
    });

data / configuration.js:

'use strict';

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

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

controller / 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 个答案:

答案 0 :(得分:1)

好的,我发现了问题!! 无处不在我使用“require(”Backbone“)”但在backbone.localStorage.js中,骨干需要“require(”backbone“)”! 所以Backbone的2个实例被缓存了,我没有使用好的那个.. 3天的战斗我已经筋疲力尽了!

希望有一天能帮助别人..