I am using ng-table for admin and user with same controller, same view but loaded data with different URL, but while getting data from cache it reloads data from cache, (which I want to clear when the 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)
This place same data loaded on both logins.Also like, If I save a form of data and redirect to ng-table the list is not updated, since it loads data from cache not from source.
You should explicitly clear cachedData array when user logs out so that new user will have fresh array of cached objects.
Or store the cachedData with key way. e.g.
cachedData=[{"user" : '123', 'data':[{}]},
"user" : '234', 'data':[{}]
]
this will add some complications as you will need to query cachedData based on user id.
Why not you use angular cache service which does this work automatically (i.e. it creates separate cache for different urls). https://github.com/jmdobry/angular-cache
Edit:To clear the cache on log out; If you have a auth controller (the controller that handles login and log out action). Inject the nameService in to that controller On click of logout you can simple write nameService.cachedData = [].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With