ui router разрешает цепочку промисов

мне нужны некоторые данные при изменении состояния с помощью ui-router, эти данные зависят друг от друга, поэтому я должен использовать цепочку обещаний

svc.getData1AndData2 = function(){
        var defer = $q.defer();
        $q.all([svc.getDatas1(),svc.getDatas2()]).then(function(values) {
            $rootScope.datas2 = values[1];
            $rootScope.datas1 = values[0];
            defer.resolve(values);
        }, function(error) {
            console.log(error);
            defer.reject(error);
        });
    return defer.promise;

svc.getData3Sources = function(){
        svc.getData1AndData2().then(function(value){
            return svc.getSources(24);
        })
    };

svc.getSources=function(id){
        var defer = $q.defer();
        Services.query({Id:id}, function(data){
                defer.resolve(data);
            };
    };

И мое состояние

.state('secure.list', {
       url: "/list",
       templateUrl: "views/list.html",
       controller: 'ListCtrl',
       resolve: {
              InitData: function (InitFactory) {
              return InitFactory.getData3Sources();
              }
        }
})

он возвращает неопределенное значение. кто знает почему?


person Silvio Troia    schedule 24.07.2014    source источник


Ответы (1)


svc.getData3Sources ничего не возвращает.

пытаться ...

svc.getData1AndData2 = function(){
    // no need to use $defer here

    var promise = $q.all([svc.getDatas1(),svc.getDatas2()]).then(function(values) {
        $rootScope.datas2 = values[1];
        $rootScope.datas1 = values[0];

        return values;
    }, function(error) {
        console.log(error);
        defer.reject(error);
    });

    return promise;
}; // this line was missing

svc.getData3Sources = function(){
    var promise = svc.getData1AndData2().then(function(values){ 
        // changed value to values in the line above 
        // as the promise in getData1AndData2 resolves  ... valueS

        // why don't you use the values from getData1AndData2 ?? 
        return svc.getSources(24);
    });

    return promise;
};

svc.getSources=function(id){
    var defer = $q.defer();

    // it looks like you're having to use $q.defer 
    // as your Service.Query takes a callback rather than returning a promise
    // if you control this then a promise would be much better
    Services.query({Id:id}, function(data){
            defer.resolve(data);
        }); // the bracket ')' here was missing

    return defer.promise; // this line was missing
};
person stooboo    schedule 24.07.2014
comment
нет, не работает! я думаю, что он возвращается при первом defer.resolve, что он работает - person Silvio Troia; 24.07.2014
comment
возвращают ли getDatas1 и getDatas2 обещания? .. если это так, то $q.all ТАКЖЕ вернет одно обещание .. нет необходимости использовать $q.defer() в getData1AndData2 - person stooboo; 25.07.2014
comment
код немного странный, так как вы не возвращаете полученные значения. getData3Sources не использует «значения» из «обещания» getData1AndData2 и т. д. - person stooboo; 25.07.2014
comment
да, getDatas1 и getDatas2 возвращают обещание. Какой правильный режим для создания цепочки обещаний в разрешении ui-router? - person Silvio Troia; 28.07.2014