Hi Jonathan,
Yes, if that is working, it’s indeed mostly luck. This is where you need to build your own promise handler.
Something like this:
.factory('Utility', function($http) {
var self = this;
self.aList = []; //placeholder
self.reAssign = function (response) { //handle putting results into the placeholder
// don't forget to handle error's in here!
self.aList = response.data;
};
// a function to enable refetching. it also uses the promise to (re)populate the aList array.
self.reftech= function () {self.pList = $http.get('getList.html').then(self.reAssign);}
// kick off first time loading!
self.refetch();
return self;
});
This will always return the promise, and the complete list. But it will only fetch only once, after that, it will just return the resolved promise.
Doing this, you have the means to check if the list is fetched already (the promise will be resolved/rejected), and the list itself readily
Available. This is similar to what ngResource does.
With kind regards
Sander
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/r-TxMo1bj14/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Hi Jonathan,
I went quickly over your plunk and found a number of small issues. Not that you did anything wrong, but often you opted
for the most expensive way (from a performance view). all those small things combined makes your solution feeling a bit sluggish.
compare those 2 functions, and then remember that those get called a lot!
getShortcutIndex = function(id) {
for (var i = 0; i < $scope.shortcut.length; i++) {
if (id === $scope.shortcut[i].SHORTCUTID) {
return i;
}
}
}
getShortcutIndex = function(id) {
var i=0 , l = $scope.shortcut.length;
while (id !== $scope.shortcut[i].SHORTCUTID && ++i<l) {
//just loop
}
//only return i if there is indeed a match.
return id !== $scope.shortcut[i].SHORTCUTID ? i : null;
};
The first one traverse the entire array every time!. Also you are generating a awful of DOM that is not needed.
I did put in a directive counter, and in your orginal version it counted 1122 shortcut-new directives being generated.
User filters and ngIf to only show the items you need on screen. In te atached updated plunk I putted in just a couple of ngIf in stead of ngShow
and the count dropped to 5!!!
There is still a lot to be won. while it still only fires 5 directives, your solution has to do the looping for all, on every change…
I would replace the second nested ngRepeat with a singe one looping over the shortcut array and use a filter to get only the ‘active’ ones.
that reduces the number of loops quite a lot!
Have a look for yourself.
here is the updated plunk: http://plnkr.co/edit/oBUCFkWndGem3PG3LHb2?p=preview
If you have additional questions, don’t hesitate to ask them!
Regards
Sander