在与 Require.js 一起使用 Backbone.js 框架时,我正在尝试将一些数据保存到 localStorage。
但是,我收到以下错误:
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 使其工作。但是有人知道我错过了什么吗?