angularjs - Manipulate the results stored within a 'promise' -


i have service wish use first grab object json file , return selected data said object, depending on user requests.

this service may used multiple times per visit, don't wan user have wait while data retrieved on every occasion.

i have set service request json file once per page load, i'm having trouble extracting data wish return.

my idea take clone of initial promise object (referred promiseall in code) , manipulate data within, before returning cloned object (referred 'promiseselected') user.

what have below works, if users requests list of type searchable, every future request has results request.

i'm not sure i'm doing wrong (or if there better way this), i'd apprciate pointers.

here how using service -

app.controller('searchctrl', ['$scope', '$localstorage', '$stationslist', function($scope, $localstorage, $stationslist){      $stationslist.getlist('searchable').then(function(data){         $scope.stationslist = data; // grab list of searchable stations     });  }]); 

and here full service -

app.service('$stationslist', ['$http', function($http, $scope){      var tempstations,         promiseall,         promiseselected;      /**      * grab list of required stations      *      * @param string type               type of list return      * @return object promiseselected   promise object containing stations requested user      */     var getstationslist = function(type){          if(!promiseall){              promiseall = $http.get('stations.json').then(function(res){                 return res.data;    // grab json list of stations             });          }          promiseselected = angular.copy(promiseall); // take fresh copy of 'promiseall'         tempstations = [];                          // reset empty array          switch(type){              case "searchable":                  promiseselected = promiseall.then(function(data){                      [].map.call(data || [], function(elm){  // map stations...                         if (elm.link.indexof(".xml") > -1)  // check see if station searchable                             tempstations.push(elm);         // - add station 'tempstations'                     });                      return tempstations;                  });                  break;              case "locatable":                  promiseselected = promiseall.then(function(data){                      [].map.call(data || [], function(elm){  // map stations...                          if(                         isfinite(parsefloat(elm.latitude)) &&                         isfinite(parsefloat(elm.longitude))                         )                                   // check see if station locatable                             tempstations.push(elm);         // - add station 'tempstations'                      });                      return tempstations;                  });                  break;              default:                 promiseselected = promiseall;          }          return promiseselected;      };      return{         getlist: getstationslist     };  }]); 

the problem reuse same tempstations variable everywhere. variable should local every function passed then().


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -