I am working with the project. I am using pagination, checkAll, deleteAll like functionalities that are common between all controllers. I wish to move the code to factory or service so can be utilize to all controllers. I tried but not clear with perfection. I want hint. I know to create factory or service, and how to intefrate it controller but bit confuse with logic of moving similar code.
I give example that I use to write in all controller, Logic for Sorting and pagination as well.
$scope.bigCurrentPage = 1;
$scope.editIndex = "";
var sortKey = "name";
var sortDirection = "ASC";
$scope.filterObj = {order: [sortKey + " " + sortDirection], limit: CONFIG.limit, offset: 0};
$scope.filterObj.offset = ($scope.bigCurrentPage - 1) * CONFIG.limit;
$scope.sortList = function (inputSort) {
if (sortKey === inputSort) {
sortDirection = (sortDirection === "ASC") ? "DESC" : "ASC";
} else {
sortKey = inputSort;
}
$scope.filterObj.order = [sortKey + " " + sortDirection];
$scope.pageChanged();
}
$scope.getSongs = function () {
$scope.filterObj.offset = ($scope.bigCurrentPage - 1) * CONFIG.limit;
Song.find({filter: $scope.filterObj})
.$promise
.then(function (data) {
$scope.items = data;
$scope.maxSize = 5;
$scope.size = $scope.filterObj.limit;
$scope.countingIndex = ($scope.bigCurrentPage - 1) * $scope.filterObj.limit; //Its idea to implement counting. Variable is not part of pagination..////
});
};
$scope.pageChanged = function () {
$scope.getSongs();
};
$scope.pageChanged();
This way, suppose I am doing this for Albums and I would change sortKey as 'id', Will use services with 'Album.find', would change 'filter' object and I am done, but fact is I am using common code in all controllers. How would I write it in better way.