我试图通过下面的安装启动并运行骨干localStorage
https://github.com/jeromegn/Backbone.localStorage
但我无法弄清楚如何将其实际集成到我的应用程序中。我已经连接了我想要存储在本地的集合(saved
),但我不确定如何保存/获取模型(重新加载时)。
(function($){
require.config({
paths: {
jquery: "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js",
underscore: "//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js",
backbone: "//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js",
localStorage: "backbone.localStorage.js"
}
});
define('saved', ['localstorage'], function(){
var saved = Backbone.Collection.extend({
localStorage: new Backbone.LocalStorage('saved')
});
return saved;
});
//---------SINGLE ENTRY MODEL----------
var Entry = Backbone.Model.extend({
defaults: function(){
return{
word: '',
definition: ''
}
}
});
//------------ENTRY MODEL COLLECTION------------
EntryList = Backbone.Collection.extend({
localStorage: new Backbone.LocalStorage('saved'),
model: Entry
});
//-----INSTANCIATE COLLECTION----
var dictionary = new EntryList();
var saved = new EntryList();
.
.
.
答案 0 :(得分:0)
实际上,当您执行以下操作时,您已经使用了localStorage:
//------------ENTRY MODEL COLLECTION------------
EntryList = Backbone.Collection.extend({
localStorage: new Backbone.LocalStorage('saved'),
model: Entry
});
您从同一个集合中启动变量dictionary
和saved
,我想您只希望saved
是localStorage?如果是的话,那就不是你的预期了。
这是因为当您在扩展Backbone.Collection时添加localStorage: new Backbone.LocalStorage('saved')
属性时,此集合将永远使用localStorage。
使用它进行保存,您只需致电saved.create({ word: 'abc', definition: 'i dun know'})
要获取已保存的条目,只需saved.fetch()
来源中的example提供了一个很好的视图
顺便说一下,你总是可以输入" localStorage"检查它是否已在浏览器控制台上启动或有任何数据(i; m使用Chrome Devtool)