Last active
August 29, 2015 14:05
-
-
Save charandas/831560ade572ad9739cd to your computer and use it in GitHub Desktop.
Main FutureStates Module for ui-router-lazy-example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define([ | |
| 'angular', | |
| 'angularUiRouter', | |
| 'uiRouterExtras', | |
| 'uiRouterExtrasStatevis', | |
| 'ocLazyLoad', | |
| 'core/module', | |
| 'core/services/settings', | |
| 'states/core' | |
| ], function (angular) { | |
| 'use strict'; | |
| return angular.module('futureStates', | |
| [ | |
| 'ui.router', | |
| 'ct.ui.router.extras', | |
| 'ct.ui.router.extras.examples.statevis', | |
| 'oc.lazyLoad', | |
| 'futureStates.core', | |
| 'futureStates.states.core' | |
| ]).config([ | |
| '$ocLazyLoadProvider', | |
| '$futureStateProvider', | |
| 'SettingsServiceProvider', | |
| function($ocLazyLoadProvider, | |
| $futureStateProvider, | |
| SettingsServiceProvider) { | |
| $ocLazyLoadProvider.config ({ | |
| debug: true, | |
| jsLoader: requirejs, | |
| loadedModules: ['futureStates'], | |
| modules: [{ | |
| reconfig: true, | |
| name: 'futureStates.states.orange', | |
| files: ['states/orange'] | |
| }, { | |
| reconfig: true, | |
| name: 'futureStates.states.apple', | |
| files: ['states/apple'] | |
| }] | |
| }); | |
| var ocLazyLoadStateFactory = function ($q, $ocLazyLoad, futureState) { | |
| var deferred = $q.defer(); | |
| $ocLazyLoad.load(futureState.module).then(function(name) { | |
| deferred.resolve(); | |
| }, function() { | |
| deferred.reject(); | |
| }); | |
| return deferred.promise; | |
| }; | |
| $futureStateProvider.stateFactory('ocLazyLoad', ocLazyLoadStateFactory); | |
| $futureStateProvider.addResolve(function ($injector) { | |
| /** | |
| * NOTE: resolves can be used for determining | |
| * which future states you actually want. | |
| * Here, we register both apples and oranges. | |
| * Try uncommenting the if/else to see | |
| * the magic of provider injected logic | |
| * | |
| * Important thing to remember: in addResolve, you have | |
| * to "return" the thenable promise chain if you want it | |
| * to actually wait on your provider's resolution. | |
| */ | |
| return $injector.invoke(SettingsServiceProvider.fruit).then( | |
| function (fruitResult) { | |
| //if (fruitResult === 'oranges') { | |
| $futureStateProvider.futureState({ | |
| 'stateName': 'app.orange', | |
| 'urlPrefix': '/orange', | |
| 'type': 'ocLazyLoad', | |
| 'module': 'futureStates.states.orange' | |
| }); | |
| //} else if (fruitResult === 'apples') { | |
| $futureStateProvider.futureState({ | |
| 'stateName': 'app.apple', | |
| 'urlPrefix': '/apple', | |
| 'type': 'ocLazyLoad', | |
| 'module': 'futureStates.states.apple' | |
| }); | |
| //} | |
| } | |
| ); | |
| }); | |
| }]); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment