簡體   English   中英

骨干localStorage沒有使用良好的同步方法

[英]Backbone localStorage doesn't use the good sync method

我為我的問題瘋狂!

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

如果我沒有使用localStorage,則此錯誤為“正常”,因為我沒有定義url屬性。 在這里,提取調用應該使用Backbone.sync(也可以是LocalStorage模塊對Backbone.localSync的覆蓋嗎?)。

但是它永遠不會進入localStorage函數! 好像從未加載過該模塊。 但是我到處都添加了一些console.log,並且ribs.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}));
};

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

好吧,我發現了問題! 我在所有使用“ require(” Backbone“)”的地方,但是在bone.localStorage.js中,需要使用“ require(” backbone“)”的骨干! 因此,緩存了2個Backbone實例,而我沒有使用好的實例。.3天的戰斗讓我筋疲力盡!

希望有一天有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM