主干本地存储不使用良好的同步方法

Backbone localStorage doesn't use the good sync method

本文关键字:同步方法 存储      更新时间:2023-09-26

我的问题快疯了!

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

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

但它永远不会进入本地存储功能!好像模块从未加载过。但是我添加了一些控制台.log到处都是,并且 backbone.localstorage 已正确加载和初始化。

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

模块/配置.js渲染函数中发生错误。

这是我的代码:

型号/配置.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')
    });

数据/配置.js :

'use strict';
// Get class dependencies
var Config = require('../models/configuration');
// export module
module.exports =  new 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}));
};

模块/配置.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;
    }
});
好的,

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

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