Skip to content

Instantly share code, notes, and snippets.

@jonatanfroes
Created February 4, 2016 17:38
Show Gist options
  • Save jonatanfroes/e1c3d91c1ddff6ea736c to your computer and use it in GitHub Desktop.
Save jonatanfroes/e1c3d91c1ddff6ea736c to your computer and use it in GitHub Desktop.
// service para buscar fotos
.factory('galleryService', function($http, $q){
//var url = 'http://www.pontovips.com.br/api.php';
var url = 'http://localhost/pontovips-app/www/api.php';
return {
getItems: function(nuItemsPerRequest, firstItem) {
return $http.get(url, {
params: {
acao: 'galerias',
max: nuItemsPerRequest,
start: firstItem
},
timeout : 5000
})
.then(function(result) {
return result.data;
},
function(error) {
if(error.status === 0) {
// $http timeout
console.log('timeout error');
}
return {
error : true,
msg : 'Timeout Error',
rows : 0,
items : []
}
});
},
doRefresh: function(totalLoaded) {
return $http.get(url, {
params: {
start: totalLoaded,
nuItems: 2
}
})
.then(function(result) {
return result.data;
},
function(error) {
return {items : []};
});
},
getPhotos: function(itemId, nuItemsPerRequest, firstItem) {
return $http.get(url, {
params: {
acao: 'fotos',
id: itemId,
max: nuItemsPerRequest,
start: firstItem
}
})
.then(function(result) {
return result.data;
});
}
}
})
//controller
.controller('galleryController', function($scope, $ionicLoading, $timeout, galleryService) {
$scope.title = "Fotos";
$ionicLoading.show();
$scope.result = null;
$scope.items = [];
$scope.hasMoreItems = true;
$scope.nuItemsPerRequest = 10;
$scope.firstItem = 0;
$scope.lasItem = 0;
//load initial list
galleryService.getItems($scope.nuItemsPerRequest, $scope.firstItem)
.then(function(data) {
$scope.result = data;
$scope.items = data.items;
$scope.firstItem = $scope.items.length + 1;
$ionicLoading.hide();
});
//infinite scroll
$scope.loadMore = function() {
galleryService.getItems($scope.nuItemsPerRequest, $scope.firstItem)
.then(function(data) {
//set the new first item
$scope.firstItem = $scope.items.length + 1;
//check if there is no more items
if(data.items.length < $scope.nuItemsPerRequest) {
$scope.hasMoreItems = false;
}
//join arrays
$scope.items.push.apply($scope.items, data.items);
$scope.$broadcast('scroll.infiniteScrollComplete');
}),
function(errorMsg) {
console.log(errorMsg);
}
};
$scope.doRefresh = function() {
galleryService.getItems($scope.nuItemsPerRequest, 0)
.then(function(data) {
if (data.items.length == 0)
{
return;
}
$scope.result = data;
$scope.items = data.items;
$scope.firstItem = $scope.items.length + 1;
$scope.$broadcast('scroll.refreshComplete');
}),
function(errorMsg) {
console.log(errorMsg);
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment