2014-05-19 118 views
-1

我只是想從一個工廠是這樣解決的承諾:角JS - 解決承諾在routeProvider

.config(['$routeProvider', '$locationProvider', '$httpProvider', 'SoundRTCProvider', function ($routeProvider, $locationProvider, $httpProvider, SoundRTCProvider) { 

    $routeProvider 
     .when('/',{ 
     templateUrl:'views/home/index.html', 
     controller:'Home', 
     resolve: { 
      load: function (configFactory) { 
      return configFactory.loadAll(); 
      } 
     } 
     }); 
}); 

則:

.factory('configFactory',['$rootScope', '$q', '$location', '$anchorScroll', '$routeParams', 'ngDialog', 'cacheService', 'sessionFactory', 'geoFactory', 'instrumentsFactory', 'genresFactory', 'langFactory', 'usersFactory', 'contactsFactory', function ($rootScope, $q, $location, $anchorScroll, $routeParams, ngDialog, cacheService, sessionFactory, geoFactory, instrumentsFactory, genresFactory, langFactory, usersFactory, contactsFactory) { 
    var initConfig = function() { 
     var deferred = $q.defer(); 
     /*CONFIG PARAMS*/ 
     $rootScope.config = {}; 

     $rootScope.config.appWs = ''; 
     $rootScope.config.appName = 'Dunno'; 

     deferred.resolve('go'); 
     return deferred.promise(); 
    }; 

    var initUserSession = function() { 
     var deferred = $q.defer(); 
     /*----- INIT USER SESSION ---*/ 
     $rootScope.session = {}; 
     /*RELOAD SESSION if logged*/ 
     if(sessionFactory.get('idUser')){ 
     usersFactory.getMyProfile().then(function(results){ 
      sessionFactory.initSession(results.data); 
      deferred.resolve(); 
     }); 
     } 
     return deferred.promise(); 
    }; 

    var initGravatar = function() { 
     var deferred = $q.defer(); 
     /*------- INIT GRAVATARS ------*/ 
     $rootScope.gravatar = {}; 
     deferred.resolve(); 
     return deferred.promise(); 
    }; 

    var initLang = function() { 
     var deferred = $q.defer(); 
     /*------LANGUAGE---------*/ 
     $rootScope.userLang = 'en_EN'; 
     $rootScope.lang = {};//get key and value from here in views 

     //If user lang doesn't exists yet 
     if(cacheService.isExpired('appLang')) { 

     langFactory.getAll($rootScope.userLang) 
     .then(function (response) { 

       if(angular.isObject(response.data) && Object.keys(response.data).length > 0) { 

       cacheService.put('appLang',response.data,$rootScope.config.minChacheTime); 
       $rootScope.lang = cacheService.get('appLang'); 

       } else { 

       $rootScope.lang = cacheService.get('appLang'); 
       } 

       deferred.resolve(); 
      }); 

     } else { 

     deferred.resolve(); 
     $rootScope.lang = cacheService.get('appLang'); 

     } 
     return deferred.promise(); 
    }; 

    var initGenres = function() { 
     var deferred = $q.defer(); 
     /*-------GENRES-------*/ 
     if(cacheService.isExpired('appGenres')) { 

     genresFactory.getAll() 
     .then(function (response) { 

      if(angular.isObject(response.data) && Object.keys(response.data).length > 0) { 

      cacheService.put('appGenres',response.data,$rootScope.config.minChacheTime); 
      $rootScope.config.appGenres = cacheService.get('appGenres'); 

      } else { 

      $rootScope.config.appGenres = cacheService.get('appGenres'); 
      } 
      deferred.resolve(); 
     }); 

     } else { 
     deferred.resolve(); 
     $rootScope.config.appGenres = cacheService.get('appGenres'); 
     } 
     return deferred.promise(); 
    }; 
    var initInstruments = function() { 
     var deferred = $q.defer(); 
     /*------INSTRUMMENTS------*/ 
     if(cacheService.isExpired('appInstruments')) { 

     instrumentsFactory.getAll() 
     .then(function (response) { 

      if(angular.isObject(response.data) && Object.keys(response.data).length > 0) { 

       cacheService.put('appInstruments',response.data,$rootScope.config.minChacheTime); 
       $rootScope.config.appInstruments = cacheService.get('appInstruments'); 

      } else { 

       $rootScope.config.appInstruments = cacheService.get('appInstruments'); 
      } 

      deferred.resolve(); 
      }); 

     } else { 
     deferred.resolve(); 
     $rootScope.config.appInstruments = cacheService.get('appInstruments'); 
     } 
     return deferred.promise(); 
    }; 
    var initGeo = function() { 
     var deferred = $q.defer(); 
     /*-------GEO----------*/ 
     if(cacheService.isExpired('appGeo')) { 

     geoFactory.getAll() 
     .then(function (response) { 

      if(angular.isObject(response.data) && Object.keys(response.data).length > 0) { 

      cacheService.put('appGeo',response.data,$rootScope.config.minChacheTime); 
      $rootScope.config.appGeo = cacheService.get('appGeo'); 

      } else { 

      $rootScope.config.appGeo = cacheService.get('appGeo'); 

      } 
      deferred.resolve(); 
     }); 
     } else { 
     deferred.resolve(); 
     $rootScope.config.appGeo = cacheService.get('appGeo'); 
     } 
     return deferred.promise(); 
    }; 

    var initUserContacts = function() { 
     var deferred = $q.defer(); 
     /*CONTACTS*/ 
     $rootScope.contacts = { 
     approved: [], 
     pending: [] 
     }; 
     if(sessionFactory.get('idUser')){ 
     contactsFactory.getMine().then(function (res) { 
      if(angular.isArray(res.data) && res.data.length > 0) { 

      for(var i in res.data) { 

       //set all my pending contacts 
       if(res.data[i].approved !== 1) { 

       $rootScope.contacts.pending.push(res.data[i].idContact); 

       } else { 
       //set all my contacts 
       $rootScope.contacts.approved.push(res.data[i].idContact); 

       } 

      } 
      } 
      deferred.resolve(); 
     }); 
     } else { 
     deferred.resolve(); 
     } 
     return deferred.promise(); 
    }; 
    var initLayout = function() { 
     var deferred = $q.defer(); 
     /*LAYOUT*/ 
     $rootScope.layout = { 
     loading:true, 
     userMenu:false 
     }; 

     deferred.resolve(); 
     return deferred.promise(); 
    }; 

    var initScroll = function() { 
     var deferred = $q.defer(); 
     //Make app scroll to top by default while navigating or use #anchor in url like http://sitess.com/someroute/?scrollTo=<:idelement:> to make it scroll to that element :id: 
     $location.hash($routeParams.scrollTo); 
     $anchorScroll(); 

     deferred.resolve(); 
     return deferred.promise(); 
    }; 

    var initDialog = function() { 
     var deferred = $q.defer(); 
     //Close any dialog 
     ngDialog.close(); 

     deferred.resolve(); 
     return deferred.promise(); 
    }; 

    var loadAll = function() { 
     var deferred = $q.defer(); 

     initConfig() 
     .then(function(){ 
     return initUserSession(); 
     }).then(function() { 
     return initLang(); 
     }).then(function() { 
     return initGenres(); 
     }).then(function() { 
     return initGeo(); 
     }).then(function() { 
     return initInstruments(); 
     }).then(function() { 
     return initUserContacts(); 
     }).then(function() { 
     return initGravatar(); 
     }).then(function() { 
     return initLayout(); 
     }).then(function() { 
     return initScroll(); 
     }).then(function() { 
     return initDialog(); 
     }).then(function() { 

     $rootScope.$on('loading:end', function(){ 
      $rootScope.layout.loading = false; 
     }); 

     $rootScope.$on('loading:progressing', function(){ 
      $rootScope.layout.loading = true; 
     }); 

     $rootScope.$on('$locationChangeError', function() { 
      //hide loading gif 
      $rootScope.layout.loading = false; 
      //hide loading gif 
      $rootScope.layout.userMenu = false; 
     }); 

     //all done with configs 
     deferred.resolve(); 
     }); 

     return deferred.promise(); 
    }; 

    return { 
     initConfig:initConfig, 
     initUserSession:initUserSession, 
     initLang:initLang, 
     initGenres:initGenres, 
     initGeo:initGeo, 
     initGravatar:initGravatar, 
     initInstruments:initInstruments, 
     initUserContacts:initUserContacts, 
     initLayout:initLayout, 
     initScroll:initScroll, 
     initDialog:initDialog, 
     loadAll:loadAll 
    }; 
    }]); 

怎麼了,爲什麼我在控制檯中看到錯誤:TypeError:對象不是函數 at initConfig(assets/js/factories.js:35:23)

這意味着第一個return deferred.promise();正在產生錯誤,我不明白什麼是錯的,任何幫助appriciated謝謝

回答

1

你正試圖執行一個正常的對象作爲一個函數(這是什麼錯誤的意思)。它應該是:return deferred.promise;而不是:return deferred.promise();

+1

天哪權:/感謝隊友要接受在幾分鐘內你的答案 – sbaaaang

+3

@BenjaminGruenbaum只是有時會試着去了解,並非所有的都是天才,有時需要一些小的支持,也如果文件是明確的;) – sbaaaang

+0

另外,你的代碼是_full_延期的反模式和承諾濫用...我沒有downvote這個答案,因爲它是微不足道的。我低估了它,因爲它提供了__bad建議___,如果你遵循它,你將累積技術債務。 –