我正在创建一个Chrome扩展程序,使用用户的Chrome书签填充jstree。我正在使用以下代码获取并修改通过chrome.bookmarks.getTree返回的json数据。我需要弄清楚如何通过将函数传递给树启动来获取和修改数据。 JsTree文档说你可以传递一个函数作为数据here。我认为可能因为回调而不可能......我不确定。有谁知道怎么做?
我当前的代码......需要更改它以包括获取和修改树初始化中的所有数据:
function initBookmarks(){
chrome.bookmarks.getTree(parseBookmarks);
}
function modifyData(o){
var string = JSON.stringify(o);
string = string.replace(/\"title\":/g, "\"text\":");
string = string.replace(/\"url\":/g, "\"type\": \"url\", \"url\":");
var obj = JSON.parse(string);
console.log('new obj is ', obj);
return obj;
}
function parseBookmarks(data){
console.log('the bookmarks are ' , data);
data = modifyData(data[0].children);
$('#bookmarks').jstree({
'core' : {
//expand_selected_onload : true,
'data' : data,
'check_callback' : function(o, n, p, i, m) { //operation, node, node_parent, node_position, more
//console.log(o,n,p,i,m);
if(m && m.dnd && m.pos !== 'i') { return false; }
if(o === "move_node" || o === "copy_node") {
if(this.get_node(n).parent === this.get_node(p).id) { return false; } //if current nodes parent matches parent nodes id, dont do anything
}
return true;
},
'themes' : {
'responsive' : false,
'variant' : 'small',
'stripes' : true
}
},
"search": {
"case_insensitive": true,
"show_only_matches" : true
},
//loaded : runTourStep,
'sort' : function(a, b) {
return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : (this.get_type(a) >= this.get_type(b) ? 1 : -1);
},
'contextmenu' : {
'items' : bookmarksContextMenu,
'select_node' : false
},
'types' : {
'default' : { 'icon' : 'images/folder-horizontal.png' },
'url' : { 'valid_children' : [], 'icon' : 'images/globe_icon.gif' }
},
'unique' : {
'duplicate' : function (name, counter) {
return name + ' ' + counter;
}
},
'plugins' : ['dnd','types','contextmenu','unique', 'search', 'wholerow'] //took out 'state' plugin
})
bookmarkMethods();
}
DOM准备就绪的初始标记......
所以我尝试过以下但是没有用!
function modifyData(o){
//that's all... no magic, no bloated framework
/*traverse(o,function(key,value){
});*/
o = o[0].children;
var string = JSON.stringify(o);
string = string.replace(/\"title\":/g, "\"text\":");
string = string.replace(/\"url\":/g, "\"type\": \"url\", \"url\":");
var obj = JSON.parse(string);
console.log('new obj is ', obj);
return obj;
}
function parseBookmarks(){
//console.log('the bookmarks are ' , data);
//data = modifyData(data[0].children);
$('#bookmarks').jstree({
'core' : {
//expand_selected_onload : true,
'data' : chrome.bookmarks.getTree(modifyData),
'check_callback' : function(o, n, p, i, m) { //operation, node, node_parent, node_position, more
//console.log(o,n,p,i,m);
//if(m && m.dnd && m.pos !== 'i') { return false; }
if(o === "move_node" || o === "copy_node") {
//if(this.get_node(n).parent === this.get_node(p).id) { return false; } //if current nodes parent matches parent nodes id, dont do anything
}
return true;
},
'themes' : {
'responsive' : false,
'variant' : 'small',
'stripes' : true
}
},
"search": {
"case_insensitive": true,
"show_only_matches" : true
},
//loaded : runTourStep,
'sort' : function(a, b) {
return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : (this.get_type(a) >= this.get_type(b) ? 1 : -1);
},
'contextmenu' : {
'items' : bookmarksContextMenu,
'select_node' : false
},
'types' : {
'default' : { 'icon' : 'images/folder-horizontal.png' },
'url' : { 'valid_children' : [], 'icon' : 'images/globe_icon.gif' }
},
'unique' : {
'duplicate' : function (name, counter) {
return name + ' ' + counter;
}
},
'plugins' : ['dnd','types','contextmenu','unique', 'search', 'wholerow'] //took out 'state' plugin
})
bookmarkMethods();
}
在DOM上准备parseBookmarks()..