从我的控制器中的工厂加载数据

时间:2015-03-13 12:56:23

标签: angularjs controller promise factory

我正在使用工厂进行我的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");
    }


});

});

我总是被记录:

  

“承诺失败”

我做错了什么?

1 个答案:

答案 0 :(得分: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");
});