ezraroi / ngJsTree

Angular Directive for the famous JS Tree
http://ezraroi.github.io/ngJsTree/
MIT License
270 stars 98 forks source link

Unable to update jsTree #88

Closed phuwin1995 closed 8 years ago

phuwin1995 commented 8 years ago

I have this jsTree:

<div js-tree="treeConfig" ng-model="googleDriveFiles" should-apply="ignoreModelChanges()" tree-events="ready:readyCB;changed:onTreeItemChanged" tree="treeInstance"></div> 

$scope.googleDriveFiles is defined like this $scope.googleDriveFiles = []; My treeConfig:

    $scope.treeConfig = {
            core : {
                multiple : false,
                animation: true,
                error : function(error) {
                    console.log(error);
                },
                check_callback : true,
                worker : true
            },
            types : {
                default : {
                    icon : 'glyphicon glyphicon-flash'
                },
                star : {
                    icon : 'glyphicon glyphicon-star'
                },
                cloud : {
                    icon : 'glyphicon glyphicon-cloud'
                }
            },
            version: 1,
            plugins : ['types','folder']
    };

My function to push data into the array:

    function fetchGoogleDriveFiles(data){
        console.log("Files:");
        console.log(data);
        for (var i = 0; i < data.length; i++) {
            var file = data[i];
            var parent = file.parents[0];
            if (parent != $scope.GDriveFolderId) parent = '#';
            if (file.mimeType != "application/vnd.google-apps.folder"){
                var fileData = {
                    "id":file.id,
                    "parent":parent,
                    "text":file.name,
                    "data":{url:handleLink(file)},
                    "state":{"opened":true}

                }
                $scope.googleDriveFiles.push(fileData);
            }

        }
        console.log($scope.googleDriveFiles);
                //$scope.treeConfig.version++; //to recreate.
    }
    function handleLink(file){
        if ("webContentLink" in file){
            return file.webContentLink;
        } else return file.webViewLink;
    }

I tried to recreate the tree after all the pushing (increase the version attribute in treeConfig), but it also doesn't work. May someone help?

phuwin1995 commented 8 years ago

After digging up the closed issues, $scope.$apply() helps.