Hello,
I am using $http.get to load a list on the main view of my application. Now that I want to display a view with item details (when you tap on one element of the list), I see that I should implement a service to share data between views.
I have looked at: http://learn.ionicframework.com/formulas/data-the-right-way/#
However it is using a static list of elements. I can implement this, but I dont see how to use this in combination with a http request to fetch JSON data.
In other words, Can you help me combine these 2 pieces of code?
.controller('AdsCtrl', function($scope, $http, ads) {
Ā $scope.name = 'World';
Ā $scope.allads = [];
Ā $scope.ads = [];
Ā
Ā $http.get('http://www.mysite.com/list.json').success(function(data) {
Ā Ā Ā $scope.allads = data;
Ā Ā Ā $scope.loadMore();
Ā });
Ā
Ā var counter = 0;
Ā
Ā $scope.loadMore = function() {
Ā Ā Ā var data = [];
Ā Ā Ā var l = $scope.ads.length;
Ā Ā Ā var m = Math.min(l+10, $scope.allads.length);
Ā Ā Ā for ( var i = l; i < m; i++) {
Ā Ā Ā Ā Ā data.push($scope.allads[i]);
Ā Ā Ā }
Ā Ā Ā
Ā Ā Ā console.log('Loading more!', m);
Ā Ā Ā $scope.ads = $scope.ads.concat(data);
Ā Ā Ā $scope.$broadcast('scroll.infiniteScrollComplete');
Ā };
Ā $scope.$on('stateChangeSuccess', function() {
Ā Ā Ā $scope.loadMore();
Ā });
Ā
Ā // THIS METHOD LOADS 4 LOCAL TEST ITEMS WITH THE ADSSERVICE
Ā //$scope.ads = ads;
})
and
.service('AdsService', function($q) {
Ā return {
Ā Ā Ā ads : [
Ā Ā Ā Ā Ā Ā { shortrelativetime:'30 min ago', title: 'TST ąøąø²ąø«ąø²ąø£ą¹ąøŖąø£ąø“ąø”ą¹ąøąøµą¹ąø”ąøąø§ąø²ąø”ąøŖąø¹ąø', group:'Vehicles', category:'Cars', province:'Vientiane Capital', district:'Xaythany', price:'24000', currency:'USD', photo:'20140202-040753-c1395444fd.jpg', id: 1 },
Ā Ā Ā Ā Ā Ā { shortrelativetime:'1 day ago', title: 'TST Samsung Galaxy Grand', group:'High Tech', category:'Phones', province:'Vientiane Capital', district:'Xaythany', price:'6000', currency:'THB', photo:'20140218-101800-1602504d17.jpg', id: 5 },
Ā Ā Ā Ā Ā Ā { shortrelativetime:'1 day ago', title: 'TST ąŗąŗ²ąŗąŗąŗ“ąŗąŗąŗøąŗąŗŖą»ąŗ²ąŗ', group:'Real Estate', category:'Land or House', province:'Vientiane Capital', district:'Xaythany', price:'850000', currency:'THB', photo:'20140715-080420-4a0ba40b89.jpg', id: 6 },
Ā Ā Ā Ā Ā Ā { shortrelativetime:'08 Jun 2014', title: 'TST HTC One', group:'High Tech', category:'Phones', province:'Vientiane Capital', district:'Xaythany', price:'12000', currency:'THB', photo:'20140604-083644-046276fe30.jpg', id: 9 }
Ā Ā Ā ],
Ā Ā Ā getAds: function() {
Ā Ā Ā Ā Ā Ā return this.ads
Ā Ā Ā },
Ā Ā Ā getAd: function(adId) {
Ā Ā Ā Ā Ā Ā var dfd = $q.defer()
Ā Ā Ā Ā Ā Ā this.ads.forEach(function(ad) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā if (ad.id === adId) dfd.resolve(ad)
Ā Ā Ā Ā Ā Ā })
Ā Ā Ā Ā Ā Ā
Ā Ā Ā Ā Ā Ā return dfd.promise
Ā Ā Ā }
Ā }
})
Thanks a lot for the tips!