Aktualisierung eckig $scope-variable innerhalb der forEach-Schleife

Ich habe eine Recht umfangreiche filter - /facettierte Suche-Anwendung.
Daten geladen werden, über eine API, und dann auf der Seite angezeigt wird mit einem ng-repeat. Es gibt auch eine google-map-Instanz mit einem marker assoziiert mit jedem item. Also die Anwendung ist sehr anspruchsvoll in Bezug auf die Berechnung. Ich denke, ich könnte eine überlastung einige Kunden mit schwächeren Maschinen am Ende.

Genug, der hintergrund. Ich habe es so funktioniert, wie ich will. Nun ich bin auf der Suche nach performance-Gewinne und mehr effektive Möglichkeiten, um meine Ziele zu erreichen.

Nun versuche ich herauszufinden, ob es besser ist, zu aktualisieren, ein $scope-variable (ein array) in einer forEach-Schleife, oder update ein eigenes array und aktualisieren Sie dann die $scope-variable nach der Schleife?

So, in diesem

$scope.myArrayWithAllTheItems = [];
$scope.myArrayWithItemKeysToBeDisplayed = [];

$http({method: 'GET', url: '/MYAPIURL'}).success(function(data, status, headers, config) {

if (angular.isArray(data)) {

    angular.forEach(data, function (item, key) {

        /* 
            some prework on item object here
        */

        //update scope variable with every forEach iteration
        $scope.myArrayWithAllTheItems.push({
            /*
            BIG OBJECT WITH MY DATA
                foo : bar,
                etc...
            */
        });

        //I store keys of the items that will be displayed on the page at any one moment
        //Changing filter controls will alter this array, not the big one containg all the data
        $scope.myArrayWithItemKeysToBeDisplayed.push(key);                    

    });


} else {
    //display error...
}

}).error(function(data, status, headers, config) {
    //display error...
});

gegen diese (ich habe die Kommentare entfernt und else-Zweige, aber andernfalls ist das bit ist identisch, außer für 2 private arrays):

$scope.myArrayWithAllTheItems = [];
$scope.myArrayWithItemKeysToBeDisplayed = [];

$http({method: 'GET', url: '/MYAPIURL'}).success(function(data, status, headers, config) {

    if (angular.isArray(data)) {

        //private array variables to be update during the loop
        var _myArrayWithAllTheItems = [],
            _myArrayWithItemKeysToBeDisplayed =[];

        angular.forEach(data, function (item, key) {

            _myArrayWithAllTheItems.push({
            });

            _myArrayWithItemKeysToBeDisplayed.push(key);                    

        });

        //now update $scope variables only after the loop
        $scope.myArrayWithAllTheItems = _myArrayWithAllTheItems;
        $scope.myArrayWithItemKeysToBeDisplayed = _myArrayWithItemKeysToBeDisplayed;


    }
});
Schreibe einen Kommentar