2016-07-05 207 views
1

我得到這個錯誤,不知道如何解決它未捕獲的RangeError:超出最大調用堆棧大小。 jstree

Uncaught RangeError: Maximum call stack size exceeded

我試圖建立與使用jstree LIB樹。數據來自兩個不同的http調用。 我得到我最初的數據是這樣的:

  $scope.treeFolders = []; 
     $scope.treeFilters = []; 
     $scope.tree = []; 
    var folders = function() { 
     $http.get("folders.json") 
     .success(function (res) { 
        angular.forEach(res, function(parent){ 
         // console.log(parent) 
         if(!("parent" in parent)){ 
          parent.parent = "#" 
         } 
        }) 

        $scope.treeFolders = res 
        console.log($scope.treeFolders) 
       }); 
     }; 
     folders(); 

    var filters = function(){ 
      $http.get("child.json") 
       .success(function (res) { 
        angular.forEach(res, function(obj){ 
         if(!("parent" in obj)){ 
          // console.log(obj.folderId) 
          obj.parent = obj.folderId; 
         } 
         // console.log(obj) 
        }); 

        $scope.treeFilters = res; 
        console.log($scope.treeFilters) 

       }); 
    }; 
    filters(); 

的console.log返回數組,現在一切都很好。然後我使用功能負荷合併兩個數組來一個超級陣列

$scope.load = function(){ 
    // method 1 
      $scope.tree = $scope.treeFolders.concat($scope.treeFilters); 
      console.log(JSON.stringify($scope.tree)) 
    // method 2  
     // $scope.treeFolders.forEach(function(item){ 
     // $scope.tree.push(item) 
     // }) 
     // $scope.treeFilters.forEach(function(item){ 
     // $scope.tree.push(item) 
     // }) 
     // console.log(JSON.stringify($scope.tree)) 
     } 

因此,大家可以看到,我試圖讓一個陣列的幫助兩種方法,這兩種工作,以及和我得到這個數組

[{ 
"id":1, 
"userId":1, 
"createdDt":"2016-06-27T13:05:03.000+0000", 
"text":"folder", 
"parent":"#" 
    },{ 
"id":2, 
"parent":1, 
"userId":1, 
"createdDt":"2016-06-27T13:26:45.000+0000", 
"text":"child folder" 
    },{ 
"id":2, 
"folderId":2, 
"folder":{"id":2,"text":"child folder"}, 
"body":"empty", 
"user":{"id":1,"name":"User 1"}, 
"userId":1, 
"createdDt":"2016-06-27T13:05:47.000+0000", 
"text":"filter 1", 
"parent":2 
    },{ 
"id":3, 
"folderId":2, 
"folder":{"id":2,"text":"child folder"}, 
"body":"nothing", 
"user":{"id":1,"name":"User 1"}, 
"userId":1,"createdDt":"2016-06-27T13:00:00.000+0000", 
"text":"filter 4", 
"parent":2 
    }] 

遵循邏輯jstree文檔,我的樹應該像

* Folder (root) 
---- * Child Folder (parent) 
    ---- * Filter 1 (Child) 
    ---- * Filter 4 (Child) 

但不是這個,我拿到的地方實在是太多了迭代的錯誤。有人能幫我找到我的錯誤嗎?先謝謝你。 Plunker已附加

回答

3

您的數組包含「id」爲2的兩個項目,其中一個項目的父項定義爲「id」:2。這可能會創建一個循環引用。

嘗試將您的第一個子項目的ID更改爲4,我在您的plunker鏈接上執行了此操作,並且樹狀圖像您正在尋找的那樣加載。

+0

哦,我得到了這個問題,非常感謝解釋..但如果我會改變它,它會破壞DB結構。例如,我有過濾器ID爲5,我也有文件夾ID爲5,我已經改變過濾器ID例如6,接下來我想刪除這個過濾器。我發送一個請求到API來刪除這個過濾器但是API看到過濾器ID是6並刪除了錯誤的過濾器,怎麼樣呢? – antonyboom

+0

我不太確定我是否遵循,但這聽起來像是API或數據源的不同問題。對於jsTree,如果要在每個節點上定義父屬性,則需要爲每個項目分配唯一的ID,並且需要避免使用父子關係的循環引用。 – Adam

+0

它有助於指定用於過濾器的每個項目的屬性?查看[JSON源數據](https://www.jstree.com/docs/json/)上的「li_attr」和「a_attr」 – Adam

相關問題