I have a custom Angular service that looks like this:
videoApp.factory('cacheLoader', function ($http, $filter, $rootScope) {
this.self = this;
return {
load: function (url, allowCache) {
if(allowCache == false || localStorage.getItem(url) && (parseInt(localStorage.getItem(url + 'time')) + 20000) < (new Date().getTime()) || (!localStorage.getItem(url) )) {
$http.get(url).success(function (data) {
$rootScope.allData = data;
$rootScope.videos = data;
$rootScope.categories = $filter('categoryFilter')(data);
if(allowCache == true && parseInt(localStorage.getItem(url + 'time')) + 20000 < (new Date().getTime() )) {
localStorage.setItem(url, JSON.stringify(data));
localStorage.setItem(url + 'time', new Date().getTime());
}
});
} else {
$rootScope.allData = JSON.parse(localStorage.getItem(url));
$rootScope.videos = JSON.parse(localStorage.getItem(url));
$rootScope.categories = $filter('categoryFilter')(JSON.parse(localStorage.getItem(url)));
}
},
getFilteredResults: function (category, data, callback) {
callback = callback || $filter('articleFilter');
$rootScope.videos = callback(category, data);
return $rootScope.videos;
}
};
});
My current dilemma is trying to call cacheLoader.getFilteredResults inside the cacheLoader.load function. Since it's a window object, I can't directly reference it. I've attempted to bind "this" to the entire module pattern, but encountered an error.
Does anyone know of a way to achieve what I'm attempting to do here?