Skip to content

Commit

Permalink
docs($state): improve stateProvider.state() docs
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Nov 13, 2014
1 parent d024027 commit e0dbb73
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 38 deletions.
10 changes: 9 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ module.exports = function (grunt) {
ngdocs: {
options: {
dest: 'site',
styles: [ 'ngdoc_assets/uirouter-docs.css' ],
html5Mode: false,
title: 'UI Router',
startPage: '/api/ui.router',
Expand All @@ -132,11 +133,18 @@ module.exports = function (grunt) {
grunt.registerTask('default', ['build', 'jshint', 'karma:unit']);
grunt.registerTask('build', 'Perform a normal build', ['concat', 'uglify']);
grunt.registerTask('dist', 'Perform a clean build', ['clean', 'build']);
grunt.registerTask('dist-docs', 'Perform a clean build and generate documentation', ['dist', 'ngdocs']);
grunt.registerTask('dist-docs', 'Perform a clean build and generate documentation', ['dist', 'ngdocs', 'widedocs']);
grunt.registerTask('release', 'Tag and perform a release', ['prepare-release', 'dist', 'perform-release']);
grunt.registerTask('dev', 'Run dev server and watch for changes', ['build', 'connect:server', 'karma:background', 'watch']);
grunt.registerTask('sample', 'Run connect server with keepalive:true for sample app development', ['connect:sample']);

grunt.registerTask('widedocs', 'Convert to bootstrap container-fluid', function () {
promising(this,
system('sed -i.bak -e \'s/class="row"/class="row-fluid"/\' -e \'s/role="main" class="container"/role="main" class="container-fluid"/\' site/index.html')
);
});


grunt.registerTask('publish-pages', 'Publish a clean build, docs, and sample to github.io', function () {
promising(this,
ensureCleanMaster().then(function () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"karma-phantomjs-launcher": "~0.1.0",
"load-grunt-tasks": "~0.4.0",
"grunt-conventional-changelog": "~1.1.0",
"grunt-ngdocs": "~0.2.6"
"grunt-ngdocs": "git://github.com/christopherthielen/grunt-ngdocs#issue-86"
},
"main": "release/angular-ui-router.js"
}
246 changes: 210 additions & 36 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,12 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* Registers a state configuration under a given state name. The stateConfig object
* has the following acceptable properties.
*
* @param {string} name A unique state name, e.g. "home", "about", "contacts".
* To create a parent/child state use a dot, e.g. "about.sales", "home.newest".
* @param {object} stateConfig State configuration object.
* @param {string|function=} stateConfig.template
* <a id='template'></a>
*
* - **`template`** - {string|function=} - html template as a string or a function that returns
* html template as a string or a function that returns
* an html template as a string which should be used by the uiView directives. This property
* takes precedence over templateUrl.
*
Expand All @@ -378,94 +381,268 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* - {array.&lt;object&gt;} - state parameters extracted from the current $location.path() by
* applying the current state
*
* <pre>template:
* "<h1>inline template definition</h1>" +
* "<div ui-view></div>"</pre>
* <pre>template: function(params) {
* return "<h1>generated template</h1>"; }</pre>
* </div>
*
* @param {string|function=} stateConfig.templateUrl
* <a id='templateUrl'></a>
*
* - **`templateUrl`** - {string|function=} - path or function that returns a path to an html
* path or function that returns a path to an html
* template that should be used by uiView.
*
* If `templateUrl` is a function, it will be called with the following parameters:
*
* - {array.&lt;object&gt;} - state parameters extracted from the current $location.path() by
* applying the current state
*
* <a id='templateProvider'></a>
* <pre>templateUrl: "home.html"</pre>
* <pre>templateUrl: function(params) {
* return myTemplates[params.pageId]; }</pre>
*
* - **`templateProvider`** - {function=} - Provider function that returns HTML content
* string.
* @param {function=} stateConfig.templateProvider
* <a id='templateProvider'></a>
* Provider function that returns HTML content string.
* <pre> templateProvider:
* function(MyTemplateService, params) {
* return MyTemplateService.getTemplate(params.pageId);
* }</pre>
*
* @param {string|function=} stateConfig.controller
* <a id='controller'></a>
*
* - **`controller`** - {string|function=} - Controller fn that should be associated with newly
* Controller fn that should be associated with newly
* related scope or the name of a registered controller if passed as a string.
* Optionally, the ControllerAs may be declared here.
* <pre>controller: "MyRegisteredController"</pre>
* <pre>controller:
* "MyRegisteredController as fooCtrl"}</pre>
* <pre>controller: function($scope, MyService) {
* $scope.data = MyService.getData(); }</pre>
*
* @param {function=} stateConfig.controllerProvider
* <a id='controllerProvider'></a>
*
* - **`controllerProvider`** - {function=} - Injectable provider function that returns
* the actual controller or string.
* Injectable provider function that returns the actual controller or string.
* <pre>controllerProvider:
* function(MyResolveData) {
* if (MyResolveData.foo)
* return "FooCtrl"
* else if (MyResolveData.bar)
* return "BarCtrl";
* else return function($scope) {
* $scope.baz = "Qux";
* }
* }</pre>
*
* @param {string=} stateConfig.controllerAs
* <a id='controllerAs'></a>
*
* - **`controllerAs`** – {string=} – A controller alias name. If present the controller will be
* A controller alias name. If present the controller will be
* published to scope under the controllerAs name.
* <pre>controllerAs: "myCtrl"</pre>
*
* @param {object=} stateConfig.resolve
* <a id='resolve'></a>
*
* - **`resolve`** - {object.&lt;string, function&gt;=} - An optional map of dependencies which
* An optional map&lt;string, function&gt; of dependencies which
* should be injected into the controller. If any of these dependencies are promises,
* the router will wait for them all to be resolved or one to be rejected before the
* controller is instantiated. If all the promises are resolved successfully, the values
* of the resolved promises are injected and $stateChangeSuccess event is fired. If any
* of the promises are rejected the $stateChangeError event is fired. The map object is:
* the router will wait for them all to be resolved before the controller is instantiated.
* If all the promises are resolved successfully, the $stateChangeSuccess event is fired
* and the values of the resolved promises are injected into any controllers that reference them.
* If any of the promises are rejected the $stateChangeError event is fired.
*
* The map object is:
*
* - key - {string}: name of dependency to be injected into controller
* - factory - {string|function}: If string then it is alias for service. Otherwise if function,
* it is injected and return value it treated as dependency. If result is a promise, it is
* resolved before its value is injected into controller.
*
* <pre>resolve: {
* myResolve1:
* function($http, $stateParams) {
* return $http.get("/api/foos/"+stateParams.fooID);
* }
* }</pre>
*
* @param {string=} stateConfig.url
* <a id='url'></a>
*
* - **`url`** - {string=} - A url with optional parameters. When a state is navigated or
* A url fragment with optional parameters. When a state is navigated or
* transitioned to, the `$stateParams` service will be populated with any
* parameters that were passed.
*
* <a id='params'></a>
*
* - **`params`** - {object=} - An array of parameter names or regular expressions. Only
* use this within a state if you are not using url. Otherwise you can specify your
* parameters within the url. When a state is navigated or transitioned to, the
* $stateParams service will be populated with any parameters that were passed.
* examples:
* <pre>url: "/home"
* url: "/users/:userid"
* url: "/books/{bookid:[a-zA-Z_-]}"
* url: "/books/{categoryid:int}"
* url: "/books/{publishername:string}/{categoryid:int}"
* url: "/messages?before&after"
* url: "/messages?{before:date}&{after:date}"</pre>
* url: "/messages/:mailboxid?{before:date}&{after:date}"
*
* @param {object=} stateConfig.views
* <a id='views'></a>
* an optional map&lt;string, object&gt; which defined multiple views, or targets views
* manually/explicitly.
*
* - **`views`** - {object=} - Use the views property to set up multiple views or to target views
* manually/explicitly.
* Examples:
*
* <a id='abstract'></a>
* Targets three named `ui-view`s in the parent state's template
* <pre>views: {
* header: {
* controller: "headerCtrl",
* templateUrl: "header.html"
* }, body: {
* controller: "bodyCtrl",
* templateUrl: "body.html"
* }, footer: {
* controller: "footCtrl",
* templateUrl: "footer.html"
* }
* }</pre>
*
* Targets named `ui-view="header"` from grandparent state 'top''s template, and named `ui-view="body" from parent state's template.
* <pre>views: {
* 'header@top': {
* controller: "msgHeaderCtrl",
* templateUrl: "msgHeader.html"
* }, 'body': {
* controller: "messagesCtrl",
* templateUrl: "messages.html"
* }
* }</pre>
*
* - **`abstract`** - {boolean=} - An abstract state will never be directly activated,
* @param {boolean=} [stateConfig.abstract=false]
* <a id='abstract'></a>
* An abstract state will never be directly activated,
* but can provide inherited properties to its common children states.
* <pre>abstract: true</pre>
*
* @param {function=} stateConfig.onEnter
* <a id='onEnter'></a>
*
* - **`onEnter`** - {object=} - Callback function for when a state is entered. Good way
* Callback function for when a state is entered. Good way
* to trigger an action or dispatch an event, such as opening a dialog.
* If minifying your scripts, make sure to use the `['injection1', 'injection2', function(injection1, injection2){}]` syntax.
* If minifying your scripts, make sure to explictly annotate this function,
* because it won't be automatically annotated by your build tools.
*
* <pre>onEnter: function(MyService, $stateParams) {
* MyService.foo($stateParams.myParam);
* }</pre>
*
* @param {function=} stateConfig.onExit
* <a id='onExit'></a>
*
* - **`onExit`** - {object=} - Callback function for when a state is exited. Good way to
* Callback function for when a state is exited. Good way to
* trigger an action or dispatch an event, such as opening a dialog.
* If minifying your scripts, make sure to use the `['injection1', 'injection2', function(injection1, injection2){}]` syntax.
* If minifying your scripts, make sure to explictly annotate this function,
* because it won't be automatically annotated by your build tools.
*
* <pre>onExit: function(MyService, $stateParams) {
* MyService.cleanup($stateParams.myParam);
* }</pre>
*
* @param {boolean=} [stateConfig.reloadOnSearch=true]
* <a id='reloadOnSearch'></a>
*
* - **`reloadOnSearch = true`** - {boolean=} - If `false`, will not retrigger the same state
* If `false`, will not retrigger the same state
* just because a search/query parameter has changed (via $location.search() or $location.hash()).
* Useful for when you'd like to modify $location.search() without triggering a reload.
* <pre>reloadOnSearch: false</pre>
*
* @param {object=} stateConfig.data
* <a id='data'></a>
*
* - **`data`** - {object=} - Arbitrary data object, useful for custom configuration.
* Arbitrary data object, useful for custom configuration. The parent state's `data` is
* prototypally inherited. In other words, adding a data property to a state adds it to
* the entire subtree via prototypal inheritance.
*
* <pre>data: {
* requiredRole: 'foo'
* } </pre>
*
* @param {object=} stateConfig.params
* <a id='params'></a>
*
* A map which optionally configures parameters declared in the `url`, or
* defines additional non-url parameters. For each parameter being
* configured, add a configuration object keyed to the name of the parameter.
*
* Each parameter configuration object may contain the following properties:
*
* - ** value ** - {object|function=}: specifies the default value for this
* parameter. This implicitly sets this parameter as optional.
*
* When UI-Router routes to a state and no value is
* specified for this parameter in the URL or transition, the
* default value will be used instead. If `value` is a function,
* it will be injected and invoked, and the return value used.
*
* *Note*: `undefined` is treated as "no default value" while `null`
* is treated as "the default value is `null`".
*
* *Shorthand*: If you only need to configure the default value of the
* parameter, you may use a shorthand syntax. In the **`params`**
* map, instead mapping the param name to a full parameter configuration
* object, simply set map it to the default parameter value, e.g.:
*
* <pre>// define a parameter's default value
* params: {
* param1: { value: "defaultValue" }
* }
* // shorthand default values
* params: {
* param1: "defaultValue",
* param2: "param2Default"
* }</pre>
*
* - ** array ** - {boolean=}: *(default: false)* If true, the param value will be
* treated as an array of values. If you specified a Type, the value will be
* treated as an array of the specified Type. Note: query parameter values
* default to a special `"auto"` mode.
*
* For query parameters in `"auto"` mode, if multiple values for a single parameter
* are present in the URL (e.g.: `/foo?bar=1&bar=2&bar=3`) then the values
* are mapped to an array (e.g.: `{ foo: [ '1', '2', '3' ] }`). However, if
* only one value is present (e.g.: `/foo?bar=1`) then the value is treated as single
* value (e.g.: `{ foo: '1' }`).
*
* <pre>params: {
* param1: { array: true }
* }</pre>
*
* - ** squash ** - {bool|string=}: `squash` configures how a default parameter value is represented in the URL when
* the current parameter value is the same as the default value. If `squash` is not set, it uses the
* configured default squash policy.
* (See {@link ui.router.util.$urlMatcherFactory#methods_defaultSquashPolicy `defaultSquashPolicy()`})
*
* There are three squash settings:
*
* - false: The parameter's default value is not squashed. It is encoded and included in the URL
* - true: The parameter's default value is omitted from the URL. If the parameter is preceeded and followed
* by slashes in the state's `url` declaration, then one of those slashes are omitted.
* This can allow for cleaner looking URLs.
* - `"<arbitrary string>"`: The parameter's default value is replaced with an arbitrary placeholder of your choice.
*
* <pre>params: {
* param1: {
* value: "defaultId",
* squash: true
* } }
* // squash "defaultValue" to "~"
* params: {
* param1: {
* value: "defaultValue",
* squash: "~"
* } }
* </pre>
*
*
* @example
* <pre>
Expand All @@ -474,7 +651,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* // stateName can be a single top-level name (must be unique).
* $stateProvider.state("home", {});
*
* // Or it can be a nested state name. This state is a child of the
* // Or it can be a nested state name. This state is a child of the
* // above "home" state.
* $stateProvider.state("home.newest", {});
*
Expand All @@ -488,9 +665,6 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* .state("contacts", {});
* </pre>
*
* @param {string} name A unique state name, e.g. "home", "about", "contacts".
* To create a parent/child state use a dot, e.g. "about.sales", "home.newest".
* @param {object} definition State configuration object.
*/
this.state = state;
function state(name, definition) {
Expand Down

0 comments on commit e0dbb73

Please sign in to comment.