2011-10-16 16 views
1

如果我JStree初始化函數是在同一個文件作爲來電顯示一切正常,但如果我將函數調用html文件它不回報什麼。這裏是我想出了到現在的代碼:裹JStree在功能和傳遞變量,如果功能是不一樣的文檔呼叫者不工作

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']); 
    } 
    ); 
+0

這將是定時/順序問題。你能提供jsfiddle樣本嗎? – Radek

+1

我相信這是在外部js加載之前調用函數的問題。 – Marius

回答

1

這裏是想出了:在外部文件

(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); 
})(); 

cattree init和頭部分的代碼或我認爲所有的HTML如下:

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

目前它的工作非常好。我認爲問題在於在加載所有內容之前調用了該函數。作爲添加Jquery.cattree到if語句它沒有認識到$ .cattree作爲一個功能之前。