markizano

makes-no-sense.js

Nov 21st, 2022
1,161
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.07 KB | Software | 0 0
  1. // BEGIN: This works!
  2. /**
  3.  * Account Institutions Controller.
  4.  * @param {angular.service.$scope} $scope Angular $scope service.
  5.  * @param {angular.service.$http} $http Angular $http service.
  6.  */
  7.     function institutions($scope, $http) {
  8.     console.log('Yaba.app.controller(institution).load()');
  9.     $scope.institutions = [{}];
  10.     $scope.institution = Yaba.models.EMPTY_Institution;
  11.     $http({
  12.         method: 'GET',
  13.         url: '/api/institutions'
  14.     }).then(function(response) {
  15.         $scope.institutions = response.data.institutions;
  16.     });
  17.     $scope.addMapping = function addMapping() {
  18.         $scope.institution.mappings.push({
  19.             fromField: null,
  20.             toField: null,
  21.             mapType: null
  22.         });
  23.     };
  24. }
  25. // END: This works!
  26.  
  27. // BEGIN: This fails
  28. class Institution {
  29.     constructor(services) {
  30.         services.hasOwnProperty('$http') && ( this.$http = services.$http );
  31.         services.hasOwnProperty('$scope') && ( this.$scope = services.$scope );
  32.     }
  33.  
  34.     /**
  35.      * Query for an institution, many institutions and get more details in list format.
  36.      * @returns Object<Institution>
  37.      */
  38.     async load() {
  39.         var result = await this.$http({
  40.             method: 'GET',
  41.             url: '/api/institutions'
  42.         });
  43.         this.$scope.institutions = result.data.institutions;
  44.         return result.data.institutions;
  45.     }
  46. }
  47.  
  48. /**
  49.  * Account Institutions Controller.
  50.  * @param {angular.service.$scope} $scope Angular $scope service.
  51.  * @param {angular.service.$http} $http Angular $http service.
  52.  */
  53. function institutions($scope, $http) {
  54.     var bank = new Yaba.models.Institution({
  55.         $scope: $scope,
  56.         $http: $http
  57.     });
  58.     console.log('Yaba.app.controller(institution).load()');
  59.     bank.load();
  60.     $scope.institution = Yaba.models.EMPTY_Institution;
  61.     $scope.addMapping = function addMapping() {
  62.         $scope.institution.mappings.push({
  63.             fromField: null,
  64.             toField: null,
  65.             mapType: null
  66.         })
  67.     };
  68. }
  69. // END: This fails.
  70. // Why?!?!
Advertisement
Comments
  • markizano
    3 years
    # text 0.14 KB | 0 0
    1. https://ivantanev.com/async-functions-and-angularjs-1-x-do-not-mix/
    2.  
    3. This is why, makes sense now. AngularJS needs to track all async events.
Add Comment
Please, Sign In to add comment