在UI路由器解决嵌套的承诺 [英] Nested promises in ui-router resolve

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

问题描述

我一直在挣扎与下列问题,而

我想之前的观点显示,(决心)来获取一些数据。但一些数据是dependend在另一个承诺的结果。

它是这样的:我从$ stateParams作业ID(指数),并期待在我服务的数据。一旦完成,从这个结果(工作),我可以查找设置和地板(分别来自不同的服务),都返回一个承诺。

我已经来到了至今

  jobinfo:功能(乔布斯楼JobSetting,$ stateParams,$ Q){    变种延迟= $ q.defer();        Jobs.getByIndex($ stateParams.index)
        。然后(功能(工作){
            的console.log('找到工作');
            $ q.all({楼层:Floor.getByJob(作业),设置:JobSetting.getByJob(作业)})
            。然后(功能(信息){
                的console.log('了信息');
                defer.resolve([工作,info.floors,info.settings]);
            });
        });    返回defer.promise;
}

取2

  jobinfo:功能(乔布斯楼JobSetting,$ stateParams,$ Q){    返回Jobs.getByIndex($ stateParams.index)
        。然后(功能(工作){
            的console.log('找到工作');
            返回$ q.all({楼层:Floor.getByJob(作业),设置:JobSetting.getByJob(作业)})
            。然后(功能(信息){
                的console.log('了信息');
                返回[工作,info.floors,info.settings]
            });
        });
}

似乎都失败了,我甚至不得到的console.log 回来了。

PS:我离开了code OUT的其余部分,很明显,他们被包裹在一个

 解析:{
...
}

和在正确的位置来定义。

有人能这么温柔点我走出了正确的方向?


我结束了该解决方案是以下几点:(感谢的 avcajaraville 的)

 工作:功能(乔布斯$ stateParams){
    返回Jobs.getByIndex($ stateParams.index);
},地板:功能(楼工作){
    返回Floor.getByJob(工作);
},设置:功能(JobSetting,工作){
    返回JobSetting.getByJob(工作);
}


解决方案

我喜欢的解析器分开。

您可以在每个解析器注入的价值是这样的:

 工作:功能(乔布斯$ stateParams,$ Q){
    变种延迟= $ q.defer();
    Jobs.getByIndex($ stateParams.index,功能(作业){
        defer.resolve(工作);
    });
    返回defer.promise;
},地板:功能(楼工作,$ Q){
    变种延迟= $ q.defer();
    Floor.getByJob(作业,功能(楼){
        defer.resolve(楼);
    });
    返回defer.promise;
},设置:功能(JobSetting,工作,$ Q){
    变种延迟= $ q.defer();
    JobSetting.getByJob(作业,功能(设置){
        defer.resolve(设置);
    });
    返回defer.promise;
},

UI路由器文档:


  

该resolve属性是一个映射对象。该地图对象包含
  的键/值对


  
  

      
  • 键 - {string}里:一个依赖的名字被注入到控制器

  •   
  • 工厂 - {串|功能}

  •   

  
  

[...]


  
  

如果函数,那么它被注入和返回值被视为
  的依赖。如果结果是一个承诺,则之前的解决
  控制器被实例化并且它的值被注入
  控制器。


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

I would like to get some data before the view shows, (resolve). But some of the data is dependend on the result of another promise.

It goes like this: I get the job id (index) from the $stateParams and look up the data in my Service. Once it is completed, from this result (the job), I can look up the settings and floors (each from a different service), both return a promise.

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;
}

Take 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];
            });
        });
}

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

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

resolve: {
...
}

and defined in the right place.

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


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);
}

解决方案

I like to separate the resolvers.

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;
},

From ui-router documentation:

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

  • key – {string}: a name of a dependency to be injected into the controller.
  • factory - {string|function}:

[...]

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路由器解决嵌套的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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