improve categories loading
define(['angular','feed-mgr/module-name','constants/AccessConstants'], function (angular,moduleName) {
angular.module(moduleName).factory('CategoriesService',["$q","$http","RestUrlService","AccessControlService","EntityAccessControlService", function ($q, $http, RestUrlService,AccessControlService,EntityAccessControlService) {
+ /**
+ * internal cache of categories
+ * @type {Array}
+ */
+ var categories = [];
+ /**
+ * the loading request for all categories
+ * @type promise
+ */
+ var loadingRequest = null;
+
+ var loading = false;
/**
* Create filter function for a query string
*/
@@ -47,8 +59,12 @@ define(['angular','feed-mgr/module-name','constants/AccessConstants'], function
}
function loadAll() {
- return $http.get(RestUrlService.CATEGORIES_URL).then(function (response) {
- return response.data.map(function (category) {
+ if (!loading) {
+ loading = true;
+ loadingRequest = $http.get(RestUrlService.CATEGORIES_URL).then(function (response) {
+ loading = false;
+ loadingRequest = null;
+ categories = response.data.map(function (category) {
category._lowername = category.name.toLowerCase();
category.createFeed = false;
//if under entity access control we need to check if the user has the "CREATE_FEED" permission associated with the selected category.
@@ -63,7 +79,22 @@ define(['angular','feed-mgr/module-name','constants/AccessConstants'], function
}
return category;
});
+ return categories;
+ }, function (err) {
+ loading = false;
});
+ return loadingRequest;
+ }
+ else {
+ if (loadingRequest != null) {
+ return loadingRequest;
+ }
+ else {
+ var defer = $q.defer();
+ defer.resolve(categories);
+ return defer.promise;
+ }
+ }
}
/**
@@ -99,7 +130,9 @@ define(['angular','feed-mgr/module-name','constants/AccessConstants'], function
var self = this;
return loadAll().then(function (categories) {
return self.categories = categories;
+ }, function (err) {
});
+
},
delete: function (category) {
var promise = $http({
@@ -153,6 +186,7 @@ define(['angular','feed-mgr/module-name','constants/AccessConstants'], function
var deferred = $q.defer();
if (self.categories.length == 0) {
loadAll().then(function (response) {
+ data.loading = false;
if (query) {
var results = response.filter(createFilterFor(query))
deferred.resolve(results);
@@ -160,6 +194,8 @@ define(['angular','feed-mgr/module-name','constants/AccessConstants'], function
else {
deferred.resolve(response);
}
+ }, function (err) {
+ data.loading = false;
});
}
else {
@@ -176,7 +212,8 @@ define(['angular','feed-mgr/module-name','constants/AccessConstants'], function
* @returns {CategoryModel} the new category model
*/
newCategory: function () {
- return {id: null,
+ return {
+ id: null,
name: null,
description: null,
icon: null, iconColor: null,
@@ -185,7 +222,8 @@ define(['angular','feed-mgr/module-name','constants/AccessConstants'], function
relatedFeedSummaries: [],
securityGroups: [],
roleMemberships: [],
- owner:null};
+ owner: null
+ };
},
/**
