ui рутер разрешава верижни обещания

имам нужда от някои данни, когато променям състоянието с помощта на 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
не, не работи! Мисля, че се връща при първото отлагане. Решавам, че работи - 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