ui-router 中的嵌套承诺解析 [英] Nested promises in ui-router resolve

查看:25
本文介绍了ui-router 中的嵌套承诺解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为以下问题苦苦挣扎:

I have been struggling for a while with the following problem:

我想在视图显示之前获取一些数据,(解决).但有些数据取决于另一个承诺的结果.

它是这样的:我从 $stateParams 获取作业 ID(索引)并在我的服务中查找数据.完成后,从这个结果(作业),我可以查找设置和楼层(每个来自不同的服务),两者都返回一个承诺.

到目前为止我想出了什么

What I've came up so far

jobinfo: function(Jobs, Floor, JobSetting, $stateParams, $q) {

    var defer = $q.defer();

        Jobs.getByIndex($stateParams.index)
        .then(function(job) {
            console.log('got jobs');
            $q.all({floors: Floor.getByJob(job), settings: JobSetting.getByJob(job)})
            .then(function(info) {
                console.log('got info');
                defer.resolve([job, info.floors, info.settings]);
            });
        });

    return defer.promise;
}

拿 2

jobinfo: function(Jobs, Floor, JobSetting, $stateParams, $q) {

    return Jobs.getByIndex($stateParams.index)
        .then(function(job) {
            console.log('got jobs');
            return $q.all({floors: Floor.getByJob(job), settings: JobSetting.getByJob(job)})
            .then(function(info) {
                console.log('got info');
                return [job, info.floors, info.settings];
            });
        });
}

两者似乎都失败了,我什至没有得到 console.log 回来.

Both seem to fail, I do not even get a console.log back.

PS:我省略了其余的代码,显然它们被包裹在一个

PS: I left the rest of the code out, obviously they are wrapped in an

resolve: {
...
}

并在正确的位置定义.

有人能这么温柔地指出我正确的方向吗?

Could someone be so gentle to point me out in the right direction ?

我最终得到的解决方案如下:(感谢 avcajaraville)

The solution I ended up with was the following: (thanks to avcajaraville)

job: function(Jobs, $stateParams) {
    return Jobs.getByIndex($stateParams.index);
},

floors: function(Floor, job) {
    return Floor.getByJob(job);
},

settings: function(JobSetting, job) {
    return JobSetting.getByJob(job);
}

推荐答案

我喜欢将解析器分开.

您可以通过这种方式在每个解析器上注入值:

You can inject the value on each resolver this way:

job : function( Jobs, $stateParams, $q ) {
    var defer = $q.defer();
    Jobs.getByIndex( $stateParams.index, function( job ) {
        defer.resolve( job );
    });
    return defer.promise;
},

floor : function( Floor, job, $q ) {
    var defer = $q.defer();
    Floor.getByJob( job, function( floor ) {
        defer.resolve( floor );
    });
    return defer.promise;
},

settings : function( JobSetting, job, $q ) {
    var defer = $q.defer();
    JobSetting.getByJob( job, function( settings ) {
        defer.resolve( settings );
    });
    return defer.promise;
},

来自 ui-router 文档:

resolve 属性是一个地图对象.地图对象包含

The resolve property is a map object. The map object contains key/value pairs of

  • key – {string}:要注入控制器的依赖项的名称.
  • 工厂 - {string|function}:

[...]

if 函数,那么它被注入并且返回值被视为依赖性.如果结果是一个promise,它会在promise之前解决控制器被实例化并且它的值被注入到控制器.

if function, then it is injected and the return value is treated as the dependency. If the result is a promise, it is resolved before the controller is instantiated and its value is injected into the controller.

这篇关于ui-router 中的嵌套承诺解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆