我有以下代码添加了一个javascript复杂对象(树),你可以看到它是一个数组数组,其值是对象本身。
我想像这样放入缓存:
localStorage.Terms = tree;
但在阅读时,在控制台中,它显示为object object
function GetTermsDataFromTaxonomy(){
var start = new Date().getTime();
//Current Context
var context = SP.ClientContext.get_current();
//Current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Term Stores
var termStores = taxSession.get_termStores();
//Name of the Term Store from which to get the Terms.
var termStore = termStores.getByName("Taxonomy_kl5tZjInn7STsFTzIE7n3Q==");
//GUID of Term Set from which to get the Terms.
var termSet = termStore.getTermSet("31da4bc1-6429-499a-9d5e-be5e18b13c87");
var terms = termSet.getAllTerms();
context.load(terms);
context.executeQueryAsync(function(){
var termEnumerator = terms.getEnumerator(), tree = {
term: terms,
children: []
};
var termList = "Terms: \n";
while(termEnumerator.moveNext()){
var currentTerm = termEnumerator.get_current();
var currentTermPath = currentTerm.get_pathOfTerm().split(';');
var children = tree.children;
// Loop through each part of the path
for (var i = 0; i < currentTermPath.length; i++) {
var foundNode = false;
for (var j = 0; j < children.length; j++) {
if (children[j].name === currentTermPath[i]) {
foundNode = true;
break;
}
}
// Select the node, otherwise create a new one
var term = foundNode ? children[j] : { name: currentTermPath[i], children: [] };
// If we're a child element, add the term properties
if (i === currentTermPath.length - 1) {
term.term = currentTerm;
term.title = currentTerm.get_name();
term.guid = currentTerm.get_id().toString();
}
// If the node did exist, let's look there next iteration
if (foundNode) {
children = term.children;
}
// If the segment of path does not exist, create it
else {
children.push(term);
// Reset the children pointer to add there next iteration
if (i !== currentTermPath.length - 1) {
children = term.children;
}
}
}
}
localStorage.Terms = tree;
var end = new Date().getTime();
var time = end - start;
console.log('Execution time: ' + time);
},
function(sender,args){
console.log(args.get_message());
});
}
function GetAllTermsRecursive(){
if(typeof(Storage) !== "undefined")
{
var lastRefreshDateFromTermStore = localStorage.LastRefreshDateFromTermStore;
//get data when the code is executed
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
var today = new Date(year, month, day, 0,0,0,0);
if(lastRefreshDateFromTermStore < today){
localStorage.removeItem('Terms');
localStorage.removeItem('LastRefreshDateFromTermStore');
}
//Check if data has been cached.
if(typeof(localStorage.Terms) !== "undefined")
{
var start = new Date().getTime();
var terms=localStorage.Terms;
var end = new Date().getTime();
var time = end - start;
console.log('Execution time with cache: ' + time);
}
else
{
//get date for when the data is cached
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
var cachedDate = new Date(year, month, day, 0,0,0,0);
localStorage.LastRefreshDateFromTermStore = cachedDate;
GetTermsDataFromTaxonomy();
}
}
else {
alert("Cache not sopported in old browsers, please upgrade");
}
}
答案 0 :(得分:1)
您只能在localStorage
中存储字符串值。每当您尝试保存Object
类型时,都会调用其toString
方法,当然会导致[object Object]
。
通常使用JSON.stringify
来保存数据(对象的字符串表示)
localStorage.Terms = JSON.stringify(tree);
当您检索数据时和JSON.parse
:
var terms = JSON.parse(localStorage.Terms);