caching - angular view not updating from shared service -


i need little bit of structuring angular app.

i have 2 controllers, 'offerlistcntrl' (displays list of offers) , 'offerdetailscntrl' (displays single offer via id) service/model called 'offers'. connect 2 web services respectively via offers service, getoffers (returns array of offers) , getoffer (returns single offer) , trying avoid calling getoffer(id) if getoffers has been called (saves service call)

myapp.factory('offers', function($http,config) { var data=[]; var offers = {     get: function () {         var promise = $http.get(config.api_end_point + "getoffers?appid=12",{cache:"true"})         .then(function (response){             data = response.data;             return response.data;         });         return promise;     },     getoffer: function(id) {         var local = _.find(data.offers, function(offer) { return offer.offerid == id });                            if(local){             console.log("local copy found:" + local.offerid);             console.log(local.description);             console.log(local);             return local;         }         else {             var promise = $http.get(config.api_end_point + "getoffer?appid=12&offerid=" + id,{cache:"true"})             .then(function (response){                 console.log("no local, calling ws");                 return response.data;             });             return promise;         }      } };   return offers; });    function offerdetailctrl($scope,$http,offers) {       offers.get().then(function (asyncdata){         $scope.offers = asyncdata.offers;     }); }]);   function offerdetailctrl($scope,$routeparams,$http,$location,offers) {      $scope.offerid = $routeparams.offerid;     offers.getoffer($scope.offerid).then(function (asyncdata){         $scope.offer = asyncdata;     }); } 

1 questions.

  1. the trouble when offers.getoffer(id) called: seems detect wether has local data or not expected, if there no local data calls webservice , fine, if there existing data, tries return offerdetailctrl , errors. can console.log contents of before returning , seems intact im confused why controller accept promise not actual object.

error: 'undefined' not function (evaluating 'offers.getoffer($scope.offerid).then(function (asyncdata){ $scope.offer = asyncdata; console.log($scope.offer); })') offerdetailctrl@http://staging.scanzap.com.au/assets/js/offers/offerdetailcontroller.js:17

if there better way doing id love hear :) thank help

try doing instead sure inject $q well.

getoffer: function(id) {         var deferred = $q.defer();         var local = _.find(data.offers, function(offer) { return offer.offerid == id });                            if(local){             console.log("local copy found:" + local.offerid);             console.log(local.description);             console.log(local);             deferred.resolve(local;)         }         else {             var promise = $http.get(config.api_end_point + "getoffer?appid=12&offerid=" + id,{cache:"true"})             .then(function (response){                 console.log("no local, calling ws");                 deferred.resolve(response.data);             });         }          return deferred.promise;      } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -