I am using an ng table for admin and user with a different source. author data is displayed in admin, before updating

I use ng-table for the administrator and user with the same controller, with the same view, but loaded data with a different url, but when receiving data from the cache, it reloads the data from the cache (which I want to clear when user logs out)

controller

myApp.controller('listArticle', ['$scope', '$filter', 'ngTableParams', 'nameService', '$rootScope', '$location', '$timeout', '$cookieStore', 'flashService', '$templateCache', function ($scope, $filter, ngTableParams, nameService, $rootScope, $location, $timeout, $cookieStore, flashService, $templateCache) { //$templateCache.removeAll(); $scope.success = {}; $scope.article = {}; $scope.article.edit = '-'; $scope.article.approve = '-'; $scope.article.view = 'view'; $scope.showAlert = true; flashService.displayAlertMessages(); $scope.tableParams = new ngTableParams( { page: 1, // show first page count: 10, // count per page sorting: {name: 'asc'} }, { total: 0, // length of data getData: function ($defer, params) { nameService.getData($defer, params, $scope.filter); }, counts: [], paginationMaxBlocks: 13 }); $scope.$watch("filter.$", function () { $scope.tableParams.reload(); }); }]); 

Service

 myApp.service("nameService",['$http','$filter','$cookieStore', '$rootScope', function($http, $filter, $cookieStore, $rootScope){ function filterData(data, filter) { return $filter('filter')(data, filter); } function orderData(data, params) { return params.sorting() ? $filter('orderBy')(data, params.orderBy()) : filteredData; } function sliceData(data, params) { return data.slice((params.page() - 1) * params.count(), params.page() * params.count()); } function transformData(data,filter,params) { return sliceData( orderData( filterData(data,filter), params ), params); } var service = { cachedData:[], getData:function($defer, params, filter) { if(service.cachedData.length>0) { var filteredData = filterData(service.cachedData,filter); transformedData = sliceData(orderData(filteredData,params),params); params.total(filteredData.length); $defer.resolve(transformedData); } else { var id = $cookieStore.get('userId'); if($rootScope.role == 1) { var url = "article/serverside/fetch-pending-list.php"; var data = ""; } else { var url = "article/serverside/fetch-list.php"; var data = {id:id}; } $http.post(url,data) .success(function(resp) { var i=0; for(i=0; i<resp.length; i++) { resp[i].status = parseInt(resp[i].status); resp[i].category = parseInt(resp[i].category); if($rootScope.role > 1) resp[i].edit = (resp[i].status == 1)?"Edit":""; else{ resp[i].approve = (resp[i].status == "2")?"Approve/Reject":""; } var j=0; var k=0; for(j=0;j<statusList.length;j++){ if(statusList[j]['id'] == resp[i].status) resp[i].status = statusList[j]['title']; } for(k=0;k<categories.length;k++){ if(categories[k]['id'] == resp[i].category) resp[i].category = categories[k]['title']; } } angular.copy(resp,service.cachedData); params.total(resp.length); var filteredData = $filter('filter')(resp, filter); transformedData = transformData(resp,filter,params); $defer.resolve(transformedData); }); } } }; return service; 

}]);

Note if(service.cachedData.length>0) The same data is placed here as on both inputs. Also, if I save the data form and redirect them to the ng table, the list is not updated, because it loads data from the cache not from the source.

+6
source share
1 answer

You must explicitly clear the cachedData array when the user logs out so that the new user has a new array of cached objects.

Or save your cached keywords. eg.

 cachedData=[{"user" : '123', 'data':[{}]}, "user" : '234', 'data':[{}] ] 

this will add some complexity as you will need to query cachedData based on user id.

Why don't you use the angular cache service, which automatically works (i.e. creates a separate cache for different URLs). https://github.com/jmdobry/angular-cache

Edit: clear the cache upon exiting the system; If you have an authorization controller (a controller that processes login and logout). Enter the name Service in this controller. When you click on the exit, you can simply write the name Service.cachedData = [].

+2
source

All Articles