ocombe / ocLazyLoad

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

Lazy load with promises #241

Closed vorillaz closed 9 years ago

vorillaz commented 9 years ago

Hello there again. I have a state in my provider and I lazy load some modules to use. I am trying to load synchronously a module and it's plugins. The plugins require the module to be established. My code works just fine I am just wondering if I kinda can improve the syntax as I hate nesting functions . Here is a sample of my code:

$stateProvider.state('app.demo', {
    url: "/app/demo",
    views: {
        "view": {
            templateUrl: '/partials/demo.html'
        }
    },
    resolve: {
        deps: ['$ocLazyLoad',

        function ($ocLazyLoad) {
            //this module should be established before its plugins
            return $ocLazyLoad.load(['module']).then(
            //YIKES!
            function () {
                return $ocLazyLoad.load(['module.plugin1', 'module.plugin2']).then(

                function () {
                    return $ocLazyLoad.load('js/ctrls/demoCtrl.js');
                })
            });
        }]
    }
})
ocombe commented 9 years ago

Sure, you could write it like this:

$stateProvider.state('app.demo', {
    url: "/app/demo",
    views: {
        "view": {
            templateUrl: '/partials/demo.html'
        }
    },
    resolve: {
        deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            //this module should be established before its plugins
            return $ocLazyLoad.load(['module', 'module.plugin1', 'module.plugin2', 'js/ctrls/demoCtrl.js'], {serie: true});
        }]
    }
})
vorillaz commented 9 years ago

I was missing the serie property. Nicely done. I hope to find somehow the time to write down a tutorial about my usecase. Cheers :)

ocombe commented 9 years ago

:+1:

jeevasusej commented 7 years ago

If we implement like this, after load how to inject the new module?