如果函数与调用者不在同一文档中,则在函数中包装JStree并传递变量不起作用

时间:2011-10-16 18:39:56

标签: javascript jstree

如果我的JStree初始化函数与调用者在同一个文件中,一切正常,但如果我将函数调用移动到html文档,则它不会返回任何内容。这是我现在提出的代码:

function cattree(treeID, treeAncestors, treeCurrent) {
        alert(treeID + treeAncestors + treeCurrent);
        $("#"+treeID).jstree({ 
            "core" : { 
                "initially_load" : treeAncestors,
            },

            "ui" :{
                "select_limit" : 1,
                "initially_select" : [treeCurrent],
            },
            "json_data" : {
                "progressive_render" : true,
                "progressive_unload" : true,
                "ajax" : {
                    "url" : "/taxonomy/catjson",
                    "dataType": "text json",
                    "data" : function (n) { 
                        return { id : n.attr ? n.attr("id") : 0 }; 
                    }
                }
            },
            "plugins" : [ "themes", "json_data", "ui" ],
        });
}

当我将其移动到文档时,我认为它不会等待具有加载功能的文档。

   $(document).ready(function(){
        cattree("demo", ['4e974c91f0282e7011000004', '4e974d92f0282ec41a00000a'], ['4e974da1f0282e2c0c000000']);
   }
   );

1 个答案:

答案 0 :(得分:1)

以下是:

外部文件中的cattree init

(function () {
    if(jQuery && jQuery.jstree && jQuery.cattree) { return; }
    (function ($) {
        $.cattree = function(treeID, treeAncestors, treeCurrent) {
            //alert(treeID + treeAncestors + treeCurrent);
            //tree initialization function to possibly allow one or more tree on page.
            $("#"+treeID).jstree({ 
                "core" : { 
                    "initially_load" : treeAncestors,
                },

                "ui" :{
                    "select_limit" : 1,
                    "initially_select" : treeCurrent,
                },
                "json_data" : {
                    "progressive_render" : true,
                    "progressive_unload" : true,
                    "ajax" : {
                        "url" : "/taxonomy/catjson",
                        "dataType": "text json",
                        "data" : function (n) { 
                            return { id : n.attr ? n.attr("id") : 0 }; 
                        }
                    }
                },
                "plugins" : [ "themes", "json_data", "ui" ],
            });

        }
    })(jQuery);
})();

头部的代码或我想的所有html:

$(document).ready(
    function(){
    $.cattree("demo", ['4e974c91f0282e7011000004', '4e974d92f0282ec41a00000a'], ['4e974da1f0282e2c0c000000']);
    }
);

目前它工作得非常好。我认为问题是在加载所有内容之前调用了该函数。在将Jquery.cattree添加到if语句之前,它无法将$ .cattree识别为函数。

相关问题