I'm using ons-lazy-repeat and trying to query from mySql. If I pre-defined the arrays it works perfectly. But I need it to be working dynamically with database hence I included httpget but it didn't work. So I tried adding alerts and realised httpget function gets called last after every other functions (configureItemScope, calculateItemHeight, countItems) are called finished.
I know I can use httpget function within configureItemScope but I will not be able to get CountItems anymore because it seems that countitem is run first before configureItemScope.
I'm so confused with using ons-lazy-repeat. And I'm a beginner with onsen-ui. Appreciate anyone to help please?
ons.bootstrap()
.controller('Ctrl', ['$scope', '$http', '$q', function($scope, $http, $q) {
$scope.channels = [{
name : '',
description : '',
category : ''
}];
$http.get(serviceURL + 'TestApp/services/getlist.php', {
timeout: $scope.canceler.promise
}).success(function(data) {
for (i = 0; i < data.length; i = i + 1) {
$scope.channels.push(data[index].name, data[index].description, data[index].category);
}
}).error(function() {
});
alert('scope.channels');
$scope.query = '';
$scope.$watch('query', function(q) {
if ($scope.timeout) {
$timeout.cancel($scope.timeout);
}
$scope.filteredChannels = $scope.channels.filter(function(channel) {
return channel.name.toLowerCase()
.indexOf(q.toLowerCase()) > -1;
});
});
$scope.MyDelegate = {
configureItemScope: function(index, itemScope) {
alert('MyDelegate');
itemScope.name = $scope.filteredChannels[index].name;
itemScope.description = $scope.filteredChannels[index].description;
itemScope.category = $scope.filteredChannels[index].category;
},
calculateItemHeight: function(index) {
alert('calculateItemHeight');
return 90;
},
countItems: function() {
alert('countItems');
return $scope.filteredChannels.length;
}
};
}]);