I need to clear a ul list of current items before making a REST call to Flickr's API which would then fill it again with 20 items. At the moment these are being run simultaneously which causes the list to remain with 0 items despite grabbing 20 new ones.
The REST call is currently sitting inside one of my services. From my searches I've found two possible solutions, $timeout and $viewContentLoaded, or possibly combining them into one solution. However, in my case I can't figure out how I would implement this for it to work properly.
Script to execute before anything else:
JQuery Code:
$('#fav-tags-list').on('click', 'li', function() {
$('#images-list').children('li').remove();
$('#fav-tags-list li').removeClass('selected');
$(this).addClass('selected');
});
The function that gets called by the ng-click (getImages):
flickrApp.service('shared', function() {
var imageGroup = [];
return {
getImages: function(tag) {
if (tag === false) {
var options = {
format: 'json',
tagmode: 'ANY'
}
}
else {
var options = {
format: 'json',
tags: tag,
tagmode: 'ANY'
}
}
$.getJSON('https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?', options, function(data) {
$.each(data.items, function(i, image) {
imageGroup.push(data.items[i]);
});
});
return imageGroup;
}
};
});
Markup for the ng-click (stripped of attributes):
<ul id="fav-tags-list">
<li ng-repeat="tag in tagsList" ng-bind="tag"
ng-click="shared.getImages(tag)"></li>
</ul>
EDIT: As requested here's how I consumed the service:
flickrApp.controller('flickrCtrl', ['$scope', '$firebase', 'shared', function($scope, $firebase, shared) {
$scope.shared = shared;
//Get a set of images on pageload
$scope.imageGroup = shared.getImages(false);
}]);
I'm not even sure this is what I'm looking for here, so I'd appreciate some guidance.
$http.getor$resource..that will create a bottle neck for digest cycle to run