如何在本地存储中存储包含所有模型的 "collection"? (backbone.js)
How to store a "collection" including all models in the local-storage? (backbone.js)
我在外部 fetch()
函数中读入了一个大的 xml 文件,它运行良好。我的问题是,如何将填充的集合保存到本地存储?如果本地存储已经存在,我该如何测试?否则 fetch()
将被执行。
我需要在集合中的什么地方添加代码? save()
? localStorage: new Backbone.LocalStorage("test_storage")
?
这个例子展示了一小段代码:
var test_collection = Backbone.Collection.extend( .. );
var test_collection_view = Backbone.View.extend( .. );
//--------------------------------------
// external fetch
//--------------------------------------
function fetch(){
return $.ajax({
url: 'TestInterface.xml',
method: 'GET',
dataType: 'xml',
success: function(response_xml) {
... //parse data to test_collection
}//end success
});//end ajax
}//end fetch
//--------------------------------------
// Initialize
//--------------------------------------
$(document).ready(function(){
$.when( fetch() ).done(function() {
var VIEW = new test_collection_view({collection: test_collection});
});//end when
});//end ready
更新:
我也可以推荐这个用于延迟的进一步步骤:
Return deferred promise object and resolve it
您可以使用 JSON.stringify()
保存您的 collection:
test_collection.fetch({
success: function(collection, response) {
// Store an array containing the attributes hash of each model
// in the collection.
var collectionJSON = collection.toJSON();
// Store the collection as a JSON string.
localStorage.setItem('collection', JSON.stringify(collectionJSON));
}
});
然后您可以使用以下方法检索您的 collection 数据:
var collectionString = localStorage.getItem('collection');
var collection = JSON.parse(collectionString);
检查 collection 是否已经存储:
if (localStorage.getItem('collection') !== null) {
// ...
}
实现这一点的最佳方法是在您的 fetch()
例程中。首先检查collection是否被存储,如果是return,否则获取它。
我在外部 fetch()
函数中读入了一个大的 xml 文件,它运行良好。我的问题是,如何将填充的集合保存到本地存储?如果本地存储已经存在,我该如何测试?否则 fetch()
将被执行。
我需要在集合中的什么地方添加代码? save()
? localStorage: new Backbone.LocalStorage("test_storage")
?
这个例子展示了一小段代码:
var test_collection = Backbone.Collection.extend( .. );
var test_collection_view = Backbone.View.extend( .. );
//--------------------------------------
// external fetch
//--------------------------------------
function fetch(){
return $.ajax({
url: 'TestInterface.xml',
method: 'GET',
dataType: 'xml',
success: function(response_xml) {
... //parse data to test_collection
}//end success
});//end ajax
}//end fetch
//--------------------------------------
// Initialize
//--------------------------------------
$(document).ready(function(){
$.when( fetch() ).done(function() {
var VIEW = new test_collection_view({collection: test_collection});
});//end when
});//end ready
更新:
我也可以推荐这个用于延迟的进一步步骤:
Return deferred promise object and resolve it
您可以使用 JSON.stringify()
保存您的 collection:
test_collection.fetch({
success: function(collection, response) {
// Store an array containing the attributes hash of each model
// in the collection.
var collectionJSON = collection.toJSON();
// Store the collection as a JSON string.
localStorage.setItem('collection', JSON.stringify(collectionJSON));
}
});
然后您可以使用以下方法检索您的 collection 数据:
var collectionString = localStorage.getItem('collection');
var collection = JSON.parse(collectionString);
检查 collection 是否已经存储:
if (localStorage.getItem('collection') !== null) {
// ...
}
实现这一点的最佳方法是在您的 fetch()
例程中。首先检查collection是否被存储,如果是return,否则获取它。