Last active
June 3, 2016 06:03
-
-
Save aciccarello/860614b49934b0a9d30b5c4d7048a295 to your computer and use it in GitHub Desktop.
Created as a proof of concept for a decorator that attaches the state config to a controller class and a factory for registering route configs from the class.
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
import {RouteConfig, registerState} from './RouteConfig'; | |
@RouteConfig({ | |
name: 'home', | |
url: '/home' | |
controller: HomePageController, | |
controllerAs: 'vm' | |
}) | |
class HomePageController { | |
} | |
angular.module('myApp', ['ui.router']) | |
.config(registerState(HomePageController)); |
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
/** | |
* Decorator for controller classes for route states. You can reference the class in the | |
* `controller` property. Name is a required property on the config if using the | |
* registerState helper. | |
* @return Function which takes a UI Router config object. | |
*/ | |
export default RouteConfig(target) { | |
return function(config) { | |
target.$uiRouteConfig = config; | |
} | |
} | |
/** | |
* Factory function to create a config function that registers an angular config function | |
* that registers the state using the value from the route config. | |
* | |
* @param target Class decorated with @RouteConfig | |
* @return A function which can be passed to angular.config to register the state on the target | |
*/ | |
export function registerState(target) { | |
let configFn = function($stateProvider) { | |
this.$stateProvider.state(getRouteConfig(target)); | |
} | |
configFn.$inject = ['$stateProvider']; | |
return configFn; | |
} | |
/** | |
* Helper function to pull the state config object off of a class. Abstracts the property | |
* name used and allows getting the config without Typescript complaining. | |
* | |
* @param target Class decorated with @RouteConfig | |
*/ | |
export function getRouteConfig(target) { | |
return taret.$uiRouteConfig; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be used as an alternative to ngParty/ng-metadata#27