【问题标题】:Backbone localStorage doesn't use the good sync methodBackbone localStorage 没有使用好的同步方法
【发布时间】:2016-05-30 17:07:09
【问题描述】:

我的问题快要疯了!

我有一个想要使用 localStorage 的模型,但是当我在视图中调用 model.fetch 时,出现错误“错误:必须指定“url”属性或函数”。

如果我没有使用 localStorage,这个错误是“正常的”,因为我没有定义 url 属性。这里的 fetch 调用应该使用 Backbone.sync(又名 Backbone.localSync 被 localStorage 模块覆盖,对吧?)。

但它永远不会进入 localStorage 函数!好像该模块从未加载过。但我在各处添加了一些 console.log,并且backbone.localstorage 已加载并正确初始化。

就像在某处重新加载了 Backbone,我失去了 Backbone.sync 上指向 localStorage.localSync 的覆盖...

modules/config.js,渲染函数中发生错误。

这是我的代码:

models/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}));
};

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

【问题讨论】:

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


    【解决方案1】:

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

    希望有一天这会对某人有所帮助..

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 2012-10-03
      • 2015-02-03
      相关资源
      最近更新 更多