0

I'm trying to expose the data obtained from the success method of a promise. In short, I don't know how to grab $scope.storedData. As it is right now, it is undefined.

genericService.js

myApp.factory('genericService', function($http){

    return $http.jsonp('http://foo.com/bar.json')

    .success(function(data){
      return data;
    })

    .error(function(err){
        return err;
     });
});

genericController.js

myApp.controller('genericController', ['$scope','genericService', 
     function($scope, genericService){

         genericService.success(function(data){
             $scope.storeData(data); 
         });

         $scope.storedData; // Undefined here.

         $scope.storeData = function(whatever){
              $scope.storedData = whatever;
         }

         console.log('data stored is: ', $scope.storedData); // Still undefined
}]);

How do I expose $scope.storedData to the scope outside of storeData() or genericService.success()?

Note: I don't want to use $watch. I want to overcome this scope issue fairly un-Angularly... because it should be possible.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
fuzzybear3965
  • 243
  • 5
  • 15
  • It's undefined because genericService is asynchronous. It will be defined once the `success` callback is fired. If you move the logger into the callback, you will see it work. – Brennan Jul 13 '15 at 02:41
  • But, I'm calling storeData() as soon as the ``success`` callback is fired. Once I get inside storeData() I am outside the context of ``success`` – fuzzybear3965 Jul 13 '15 at 02:43
  • You can't have 2 success callbacks – charlietfl Jul 13 '15 at 02:55
  • Sure you can! I have tested this. It works. What I'm actually doing is executing a callback on the `promise = genericService.success('someurl') `. So, I'm returning the promise, which is `$http.jsonp('http://foo.com/bar.json')`, to `genericService()` (which is a function). Thus, executing `genericService()` would return a promise. Promises have two methods: `success()` and `error()`. I am simply providing one callback to the `sucess()` method in **genericService.js** and another in **genericController.js**. They don't conflict with each other. – fuzzybear3965 Jul 13 '15 at 21:40

1 Answers1

0

There are 2 things I typically do:

  1. I use models that define the expected response and will generally init my controller with an empty model.
  2. I use a variable to track my state.

Here's an example of what my controller might look like:

myApp.controller('genericController', GenericController);

GenericController.$inject = [
   '$scope',
   'genericService'
 ];

 function GenericController(
    $scope,
    genericService
 ) {
       $scope.loadData = loadData;
       $scope.storeData = storeData;

       init();

       ///////////////////

       function init() {
           $scope.isLoaded = false;
           $scope.storedData = {}; // if you use a model class, a new instance of this works best.
       }

       function loadData() {
           genericService.success(function(data){
               $scope.storeData(data); 
               $scope.isLoaded = true;
           });
       }

       function storeData(whatever) {
           $scope.storedData = whatever;
       }
 }
Travis
  • 5,021
  • 2
  • 28
  • 37
  • So Travis, if you were to write another function `printData(data){console.log(data);}` and tried to execute this function after the `genericService.sucess()` method had finished executing would `$scope.storedData` be defined? You seemed to have changed the value of `$scope.storedData` in `storeData(whatever)`. Essentially, what I want to do is store the data returned by the `sucess()` method to `$scope.storedData` so that it's available after the server has responded to my request for information. – fuzzybear3965 Jul 13 '15 at 23:16
  • @fuzzybear3965: `.success()` doesn't return the data. And no, this is [totally impossible](http://stackoverflow.com/q/14220321/1048572), as the callback will be executed asynchronously. – Bergi Jul 14 '15 at 10:28
  • 1
    @Bergi, thanks for clearing my question up in two sentences. To be honest, I didn't try the answers. They didn't seem like they would work. Your response has not been in vain. Your link got me all these links that I'm now reading: [this one](http://blog.slaks.net/2015-01-04/async-method-patterns/) and [this one](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) and [this one](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – fuzzybear3965 Jul 14 '15 at 23:11
  • [cont...] and [this one](http://stackoverflow.com/questions/10058814/get-data-from-fs-readfile/10058879) . – fuzzybear3965 Jul 14 '15 at 23:12