主干本地存储不使用良好的同步方法
Backbone localStorage doesn't use the good sync method
我的问题快疯了!
我有一个想要使用 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天战斗这个我已经筋疲力尽了!
希望有一天这会帮助某人..
相关文章:
- strongloop script.js run find in before delete方法:如何同步运行异步方法
- 将JSON存储和恢复到此Ionic应用程序的最有效方法
- 有没有更好的方法将音频与视频同步(只需将其放在视频中即可)
- 在 Backbone.js 中创建自定义“同步”方法
- 方法存储数据,直到浏览器关闭
- 如何将嵌套_.each转换为同步方法?首选使用 Promise 或 Async.waterfall
- 我们可以将淡出方法存储在变量中吗?
- 主干本地存储不使用良好的同步方法
- 如何在 Javascript 中编写阻塞同步方法
- 将 .css() 方法存储在变量中
- 如何调用父主干同步方法
- Javascript:初始方法存储在原型中
- 对于node.js中的本地文件,我应该使用异步文件IO方法而不是同步方法吗?
- 覆盖骨干网中的同步方法
- 同步方法和异步方法的区别是什么?
- 主干:使用自定义同步方法调用success
- 如何在同步方法中使用异步方法
- 如何构建嵌套的backbone.js模型,这些模型可以被不同的底层同步方法重用
- JavaScript:将函数方法存储到变量
- 当我们将一个方法存储在一个变量中,然后调用它时,为什么上下文对象会变为全局窗口对象