ocombe / ocLazyLoad

Lazy load modules & components in AngularJS
https://oclazyload.readme.io
MIT License
2.63k stars 510 forks source link

How can build chained resolves? #289

Closed abelrgr closed 8 years ago

abelrgr commented 8 years ago

Hi, I'm looking trough the documentation and cannot find a way to build chained resolves:

state('products', {
    url: "/products",
    views: {
        "main": {
            templateUrl: 'products/list.html',
            controller: 'productsCtrl',
        }
    },
    resolve: {
        loadProducts: ['$timeout', '$rootScope', function($timeout, $rootScope) {
            // dummy delay for testing
            $timeout(function() {
                $rootScope.products = [1, 2, 3];
                return true;
            }, 3000);
        }],
        loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
            // the $rootScope are never loaded for the controller after the 3 seconds of delay
            return $ocLazyLoad.load({
                files: ['products/list.js']
            });
        }]
    }
})

I want to wait for the "loadProducts" first to enter to the "loadMyCtrl" resolve

ocombe commented 8 years ago

Easy, just use the dependency injection to include one resolve into the other (and don't forget to return $timeout since it's a promise):

state('products', {
    url: "/products",
    views: {
        "main": {
            templateUrl: 'products/list.html',
            controller: 'productsCtrl',
        }
    },
    resolve: {
        loadProducts: ['$timeout', '$rootScope', function($timeout, $rootScope) {
            // dummy delay for testing
            return $timeout(function() {
                $rootScope.products = [1, 2, 3];
                return true;
            }, 3000);
        }],
        loadMyCtrl: ['$ocLazyLoad', 'loadProducts', function($ocLazyLoad, loadProducts) {
            return $ocLazyLoad.load('products/list.js');
        }]
    }
});
abelrgr commented 8 years ago

Works great, thanks :)