我正在尝试将一些数据保存到localStorage,同时与Require.js一起处理Backbone.js框架。
但是,我收到以下错误:
Uncaught Error: A "url" property or function must be specified
任何可能导致此错误的线索? 一个快速的谷歌透露,必须设置Backbone.sync,但localStorage适配器是否已经为我做了这个?我错过了什么?
以下是一些相关的代码段:
require.config({
paths: {
// Major libraries
jquery: 'libs/jquery/jquery.min',
underscore: 'libs/underscore/underscore.min',
backbone: 'libs/backbone/backbone.min',
// Require.js plugins
text: 'libs/require/text',
// Backbone.js plugins
localstorage: 'libs/backbone/localstorage.min',
// Just a short cut so we can put our html outside the js dir
// When you have HTML/CSS designers this aids in keeping them out of the js directory
templates: '../templates'
}
});
...
define([
'underscore',
'backbone',
'models/score',
'localstorage'
], function(_, Backbone, ScoreModel, Store){
var ScoreCollection = Backbone.Collection.extend({
model: ScoreModel,
localStorage: new Store("ScoreCollection")
});
return ScoreCollection;
});
...
define([
'underscore',
'backbone'
], function(_, Backbone){
var ScoreModel = Backbone.Model.extend({
});
return ScoreModel;
});
- 编辑 -
路线如下:
define([
'jquery',
'underscore',
'backbone',
'views/scores/list',
'views/scores/add'
], function($, _, Backbone, ScoreListView, ScoreAddView){
var AppRouter = Backbone.Router.extend({
routes: {
'scores': 'showScores',
'scores/add': 'addScore',
'*actions': 'defaultAction'
}
});
var initialize = function(){
var app_router = new AppRouter;
app_router.on('route:showScores', function(){
var scoreListView = new ScoreListView();
scoreListView.render();
});
app_router.on('route:addScore', function(){
var scoreAddView = new ScoreAddView();
scoreAddView.render();
});
app_router.on('route:defaultAction', function(){
console.log('No route');
});
Backbone.history.start();
}
return {
initialize: initialize
};
});
- 更新 -
显然,该模型似乎与该集合无关。因此,集合中设置的localStorage不会应用于模型。在模型上显式设置localStorage使其工作。但有人知道我错过了什么吗?
答案 0 :(得分:0)
This error。 Backbone.sync
由其他函数隐式调用,例如保存模型。
如果您使用的是服务器端存储,则需要在模型上设置网址。但是,使用localStorage,您需要完全覆盖Backbone.sync
方法,以便save,update和destroy函数转换为localStorage适配器上的操作。我没有使用骨干localStorage插件,但看起来它应该覆盖Backbone.sync
:https://github.com/jeromegn/Backbone.localStorage/blob/master/backbone.localStorage.js#L208
尝试一些健全性检查以确保插件已加载并且正在执行覆盖功能。