From b49ddf9848952a95032e9962796ec3a6e50857de Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Wed, 29 Feb 2012 14:56:29 -0800 Subject: docs($route, ng:view): Fix the examples to work on jsfiddle, update docs --- src/service/route.js | 198 ++++++++++++++++++++++++++++++--------------------- src/widgets.js | 113 +++++++++++++++++------------ 2 files changed, 182 insertions(+), 129 deletions(-) (limited to 'src') diff --git a/src/service/route.js b/src/service/route.js index 50f335a2..3b135e65 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -1,74 +1,22 @@ 'use strict'; + /** * @ngdoc object - * @name angular.module.ng.$route - * @requires $location - * @requires $routeParams - * - * @property {Object} current Reference to the current route definition. - * @property {Array.} routes Array of all configured routes. + * @name angular.module.ng.$routeProvider + * @function * * @description - * Watches `$location.url()` and tries to map the path to an existing route - * definition. It is used for deep-linking URLs to controllers and views (HTML partials). - * - * The `$route` service is typically used in conjunction with {@link angular.module.ng.$compileProvider.directive.ng:view ng:view} - * widget and the {@link angular.module.ng.$routeParams $routeParams} service. * - * @example - This example shows how changing the URL hash causes the $route - to match a route against the URL, and the [[ng:include]] pulls in the partial. - - - - - -
- Choose: - Moby | - Moby: Ch1 | - Gatsby | - Gatsby: Ch4
-
$location.path() = {{$location.path()}}
-
$route.current.template = {{$route.current.template}}
-
$route.current.params = {{$route.current.params}}
-
$route.current.scope.name = {{$route.current.scope.name}}
-
$routeParams = {{$routeParams}}
-
- -
-
- - -
+ * Used for configuring routes. See {@link angular.module.ng.$route $route} for an example. */ function $RouteProvider(){ var routes = {}; /** * @ngdoc method - * @name angular.module.ng.$route#when - * @methodOf angular.module.ng.$route + * @name angular.module.ng.$routeProvider#when + * @methodOf angular.module.ng.$routeProvider * * @param {string} path Route path (matched against `$location.hash`) * @param {Object} route Mapping information to be assigned to `$route.current` on route @@ -97,15 +45,8 @@ function $RouteProvider(){ * - `[reloadOnSearch=true]` - {boolean=} - reload route when only $location.search() * changes. * - * If the option is set to false and url in the browser changes, then - * $routeUpdate event is emited on the current route scope. You can use this event to - * react to {@link angular.module.ng.$routeParams} changes: - * - * function MyCtrl($route, $routeParams) { - * this.$on('$routeUpdate', function() { - * // do stuff with $routeParams - * }); - * } + * If the option is set to `false` and url in the browser changes, then + * `$routeUpdate` event is broadcasted on the root scope. * * @returns {Object} route object * @@ -121,8 +62,8 @@ function $RouteProvider(){ /** * @ngdoc method - * @name angular.module.ng.$route#otherwise - * @methodOf angular.module.ng.$route + * @name angular.module.ng.$routeProvider#otherwise + * @methodOf angular.module.ng.$routeProvider * * @description * Sets route definition that will be used on route change when no other route definition @@ -137,6 +78,106 @@ function $RouteProvider(){ this.$get = ['$rootScope', '$location', '$routeParams', function( $rootScope, $location, $routeParams) { + + /** + * @ngdoc object + * @name angular.module.ng.$route + * @requires $location + * @requires $routeParams + * + * @property {Object} current Reference to the current route definition. + * @property {Array.} routes Array of all configured routes. + * + * @description + * Is used for deep-linking URLs to controllers and views (HTML partials). + * It watches `$location.url()` and tries to map the path to an existing route definition. + * + * You can define routes through {@link angular.module.ng.$routeProvider $routeProvider}'s API. + * + * The `$route` service is typically used in conjunction with {@link angular.module.ng.$compileProvider.directive.ng:view ng:view} + * directive and the {@link angular.module.ng.$routeParams $routeParams} service. + * + * @example + This example shows how changing the URL hash causes the `$route` to match a route against the + URL, and the `ng:view` pulls in the partial. + + Note that this example is using {@link angular.module.ng.$compileProvide.directive.script inlined templates} + to get it working on jsfiddle as well. + + + + + + + + + +
+ Choose: + Moby | + Moby: Ch1 | + Gatsby | + Gatsby: Ch4 | + Scarlet Letter
+ + +
+ +
$location.path() = {{$location.path()}}
+
$route.current.template = {{$route.current.template}}
+
$route.current.params = {{$route.current.params}}
+
$route.current.scope.name = {{$route.current.scope.name}}
+
$routeParams = {{$routeParams}}
+
+
+ + it('should load and compile correct template', function() { + element('a:contains("Moby: Ch1")').click(); + var content = element('.doc-example-live ng\\:view').text(); + expect(content).toMatch(/controller\: ChapterCntl/); + expect(content).toMatch(/Book Id\: Moby/); + expect(content).toMatch(/Chapter Id\: 1/); + + element('a:contains("Scarlet")').click(); + content = element('.doc-example-live ng\\:view').text(); + expect(content).toMatch(/controller\: BookCntl/); + expect(content).toMatch(/Book Id\: Scarlet/); + }); + +
+ */ + /** * @ngdoc event * @name angular.module.ng.$route#$beforeRouteChange @@ -147,12 +188,6 @@ function $RouteProvider(){ * * @param {Route} next Future route information. * @param {Route} current Current route information. - * - * The `Route` object extends the route definition with the following properties. - * - * * `scope` - The instance of the route controller. - * * `params` - The current {@link angular.module.ng.$routeParams params}. - * */ /** @@ -165,12 +200,6 @@ function $RouteProvider(){ * * @param {Route} current Current route information. * @param {Route} previous Previous route information. - * - * The `Route` object extends the route definition with the following properties. - * - * * `scope` - The instance of the route controller. - * * `params` - The current {@link angular.module.ng.$routeParams params}. - * */ /** @@ -196,8 +225,11 @@ function $RouteProvider(){ * @methodOf angular.module.ng.$route * * @description - * Causes `$route` service to reload (and recreate the `$route.current` scope) upon the next - * eval even if {@link angular.module.ng.$location $location} hasn't changed. + * Causes `$route` service to reload the current route even if + * {@link angular.module.ng.$location $location} hasn't changed. + * + * As a result of that, {@link angular.module.ng.$compileProvider.directive.ng:view ng:view} + * creates new scope, reinstantiates the controller. */ reload: function() { dirty++; diff --git a/src/widgets.js b/src/widgets.js index 66ea7fef..8bfd232d 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -475,61 +475,82 @@ var ngNonBindableDirective = valueFn({ terminal: true }); * * @description * # Overview - * `ng:view` is a widget that complements the {@link angular.module.ng.$route $route} service by + * `ng:view` is a directive that complements the {@link angular.module.ng.$route $route} service by * including the rendered template of the current route into the main layout (`index.html`) file. * Every time the current route changes, the included view changes with it according to the * configuration of the `$route` service. * - * This widget provides functionality similar to {@link angular.module.ng.$compileProvider.directive.ng:include ng:include} when - * used like this: - * - * - * - * - * # Advantages - * Compared to `ng:include`, `ng:view` offers these advantages: - * - * - shorter syntax - * - more efficient execution - * - doesn't require `$route` service to be available on the root scope - * * * @example - - -
- overview | - bootstrap | - undefined - -
- - The view is included below: -
- -
+ + + + + + + +
+ Choose: + Moby | + Moby: Ch1 | + Gatsby | + Gatsby: Ch4 | + Scarlet Letter
+ + +
+ +
$location.path() = {{$location.path()}}
+
$route.current.template = {{$route.current.template}}
+
$route.current.params = {{$route.current.params}}
+
$route.current.scope.name = {{$route.current.scope.name}}
+
$routeParams = {{$routeParams}}
+
- it('should load templates', function() { - element('.doc-example-live a:contains(overview)').click(); - expect(element('.doc-example-live ng\\:view').text()).toMatch(/Developer Guide: Overview/); - - element('.doc-example-live a:contains(bootstrap)').click(); - expect(element('.doc-example-live ng\\:view').text()).toMatch(/Developer Guide: Initializing Angular: Automatic Initialization/); + it('should load and compile correct template', function() { + element('a:contains("Moby: Ch1")').click(); + var content = element('.doc-example-live ng\\:view').text(); + expect(content).toMatch(/controller\: ChapterCntl/); + expect(content).toMatch(/Book Id\: Moby/); + expect(content).toMatch(/Chapter Id\: 1/); + + element('a:contains("Scarlet")').click(); + content = element('.doc-example-live ng\\:view').text(); + expect(content).toMatch(/controller\: BookCntl/); + expect(content).toMatch(/Book Id\: Scarlet/); });
-- cgit v1.2.3