2015-03-13 65 views
0

我正在使用工厂来完成我的API调用,并且希望在我的控制器的其他部分发生之前发生这些调用。从我的控制器的工厂中加载数据

这里是工厂代码:

define(['dashboard/module', 'lodash'], function (module) 
{ 

'use strict'; 

module.registerFactory('httpApiCallService', function ($http, $q) 
{ 
    var assetsInUse, assetsCoupled; 
    var api = { assetsInUse: null, assetsCoupled: null }; 
    return { 

     getData: function() 
     { 
      $http.get('/v2/api/inventory/assets/count').success(function (data, status, headers, config) 
      { 
       var totalAssets = data; 
       assetsInUse = { total: data, setup: null }; 
       assetsCoupled = { total: data }; 


       $http.get('/v2/api/usage/unused/count').success(function (data, status, headers, config) 
       { 
        assetsInUse.setup = [ 
           { 
            value: totalAssets - data, 
            color: "#1675a9", 
            highlight: "#1675a9", 
            label: "is in use" 
           }, 
           { 
            value: data, 
            color: "#7eb3cf", 
            highlight: "#1675a9", 
            label: "is not used" 
           } 
        ] 
        api.assetsInUse = assetsInUse; 
        api.assetsCoupled = assetsCoupled; 
        console.log(api); 
        return api; 

       }).error(function (data, status, headers, config) 
       { 
        return alert("Something went wrong."); 
       }); 
      }).error(function (data, status, headers, config) 
      { 
      return alert("Something went wrong."); 
      }); 
     } 
    } 
}); 
}); 

我想控制器的其余部分之前打电话给我厂执行:

define(['dashboard/module', 'lodash'], function (module, _) { 

'use strict'; 

module.registerController('DashboardCtrl', function ($scope, $interval, $controller, $http, $q, 
       SmartMapStyle, uiGmapGoogleMapApi, SmartMapInstances, httpApiCallService) 
{ 
    //Data 

    var defer = $q.defer(); 

    defer.promise.then(function() { 
     console.log('we are doing stuff'); 
    }); 

    if (httpApiCallService.getData()) 
    { 
     defer.resolve(); 
    } 
    else 
    { 
     console.log("promise failed"); 
    } 


}); 

});

我总是登录:

“的承诺失败”

我在做什么错?

+0

你得到在控制台的任何错误? – Carnivorus 2015-03-13 13:00:46

+0

不,没有关于这部分的应用程序的任何错误 – Sidneyvp 2015-03-13 13:03:13

+1

你的getData()方法不返回任何东西,所以你总是得到未定义,这是虚假的。 – 2015-03-13 13:51:10

回答

0

你的getData方法也在做异步http调用。 getData不会返回任何结果,并且您的条件总是失败如果你只想在所有的http调用成功时执行其他控制器的东西,你应该在getData中传递你的promise。它可能看起来像:

module.registerFactory('httpApiCallService', function ($http, $q) 
{ 
    var assetsInUse, assetsCoupled; 
    var api = { assetsInUse: null, assetsCoupled: null }; 
    return { 

     getData: function(promiseToResolve) //Here we are sending promise to resolve when everything will be ready 
     { 
      $http.get('/v2/api/inventory/assets/count').success(function (data, status, headers, config) 
      { 
       var totalAssets = data; 
       assetsInUse = { total: data, setup: null }; 
       assetsCoupled = { total: data }; 


       $http.get('/v2/api/usage/unused/count').success(function (data, status, headers, config) 
       { 
        assetsInUse.setup = [ 
           { 
            value: totalAssets - data, 
            color: "#1675a9", 
            highlight: "#1675a9", 
            label: "is in use" 
           }, 
           { 
            value: data, 
            color: "#7eb3cf", 
            highlight: "#1675a9", 
            label: "is not used" 
           } 
        ] 
        api.assetsInUse = assetsInUse; 
        api.assetsCoupled = assetsCoupled; 
        console.log(api); 
       //instead of returning api we resolving the promise with 'api' 
        promiseToResolve.resolve(api); 

       }).error(function (data, status, headers, config) 
       { 
        promiseToResolve.reject(); 
        return alert("Something went wrong."); 
       }); 
      }).error(function (data, status, headers, config) 
      { 
       promiseToResolve.reject(); 
       return alert("Something went wrong."); 
      }); 
     } 
    } 
}); 
}); 

和控制器:

var defer = $q.defer(); 

httpApiCallService.getData(defer) ; 

defer.promise.then(function (api) { 
    console.log('we are doing stuff and can use api received from backend'); 
}) 
.catch(function(){ 
    console.log("getData failed and we cannot continue"); 
}); 
+0

非常感谢! – Sidneyvp 2015-03-13 14:54:33

相关问题