Skip to content

Instantly share code, notes, and snippets.

@aciccarello
Last active June 3, 2016 06:03
Show Gist options
  • Save aciccarello/860614b49934b0a9d30b5c4d7048a295 to your computer and use it in GitHub Desktop.
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.
import {RouteConfig, registerState} from './RouteConfig';
@RouteConfig({
name: 'home',
url: '/home'
controller: HomePageController,
controllerAs: 'vm'
})
class HomePageController {
}
angular.module('myApp', ['ui.router'])
.config(registerState(HomePageController));
/**
* 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;
}
@aciccarello
Copy link
Author

Can be used as an alternative to ngParty/ng-metadata#27

@aciccarello
Copy link
Author

@Toxantron
Copy link

You have a typo in line 35.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment