diff options
| author | Misko Hevery | 2012-02-15 15:45:07 -0800 | 
|---|---|---|
| committer | Vojta Jina | 2012-02-28 17:46:58 -0800 | 
| commit | f16bd2f747ed94547eabdc4c337775a22a365255 (patch) | |
| tree | bffa267eec9e90a8f1bc12c0063402c4c021d685 /src | |
| parent | ef7346ff70c745178d5c615e6ae5e559a4549f32 (diff) | |
| download | angular.js-f16bd2f747ed94547eabdc4c337775a22a365255.tar.bz2 | |
refactor($route): move when/otherwise to provider
Diffstat (limited to 'src')
| -rw-r--r-- | src/service/route.js | 145 | 
1 files changed, 73 insertions, 72 deletions
diff --git a/src/service/route.js b/src/service/route.js index 932e26d5..7cdcfd05 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -63,6 +63,78 @@      </doc:example>   */  function $RouteProvider(){ +  var routes = {}; + +  /** +   * @ngdoc method +   * @name angular.module.ng.$route#when +   * @methodOf angular.module.ng.$route +   * +   * @param {string} path Route path (matched against `$location.hash`) +   * @param {Object} route Mapping information to be assigned to `$route.current` on route +   *    match. +   * +   *    Object properties: +   * +   *    - `controller` – `{function()=}` – Controller fn that should be associated with newly +   *      created scope. +   *    - `template` – `{string=}` – path to an html template that should be used by +   *      {@link angular.module.ng.$compileProvider.directive.ng:view ng:view} or +   *      {@link angular.module.ng.$compileProvider.directive.ng:include ng:include} widgets. +   *    - `redirectTo` – {(string|function())=} – value to update +   *      {@link angular.module.ng.$location $location} path with and trigger route redirection. +   * +   *      If `redirectTo` is a function, it will be called with the following parameters: +   * +   *      - `{Object.<string>}` - route parameters extracted from the current +   *        `$location.path()` by applying the current route template. +   *      - `{string}` - current `$location.path()` +   *      - `{Object}` - current `$location.search()` +   * +   *      The custom `redirectTo` function is expected to return a string which will be used +   *      to update `$location.path()` and `$location.search()`. +   * +   *    - `[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 +   *              }); +   *            } +   * +   * @returns {Object} route object +   * +   * @description +   * Adds a new route definition to the `$route` service. +   */ +  this.when = function(path, route) { +    var routeDef = routes[path]; +    if (!routeDef) routeDef = routes[path] = {reloadOnSearch: true}; +    if (route) extend(routeDef, route); // TODO(im): what the heck? merge two route definitions? +    return routeDef; +  }; + +  /** +   * @ngdoc method +   * @name angular.module.ng.$route#otherwise +   * @methodOf angular.module.ng.$route +   * +   * @description +   * Sets route definition that will be used on route change when no other route definition +   * is matched. +   * +   * @param {Object} params Mapping information to be assigned to `$route.current`. +   */ +  this.otherwise = function(params) { +    this.when(null, params); +  }; + +    this.$get = ['$rootScope', '$location', '$routeParams', '$controller',        function( $rootScope,  $location,  $routeParams, $controller) {      /** @@ -112,8 +184,7 @@ function $RouteProvider(){       * instance of the Controller.       */ -    var routes = {}, -        matcher = switchRouteMatcher, +    var matcher = switchRouteMatcher,          parentScope = $rootScope,          dirty = 0,          forceReload = false, @@ -138,76 +209,6 @@ function $RouteProvider(){            /**             * @ngdoc method -           * @name angular.module.ng.$route#when -           * @methodOf angular.module.ng.$route -           * -           * @param {string} path Route path (matched against `$location.hash`) -           * @param {Object} route Mapping information to be assigned to `$route.current` on route -           *    match. -           * -           *    Object properties: -           * -           *    - `controller` – `{function()=}` – Controller fn that should be associated with newly -           *      created scope. -           *    - `template` – `{string=}` – path to an html template that should be used by -           *      {@link angular.module.ng.$compileProvider.directive.ng:view ng:view} or -           *      {@link angular.module.ng.$compileProvider.directive.ng:include ng:include} widgets. -           *    - `redirectTo` – {(string|function())=} – value to update -           *      {@link angular.module.ng.$location $location} path with and trigger route redirection. -           * -           *      If `redirectTo` is a function, it will be called with the following parameters: -           * -           *      - `{Object.<string>}` - route parameters extracted from the current -           *        `$location.path()` by applying the current route template. -           *      - `{string}` - current `$location.path()` -           *      - `{Object}` - current `$location.search()` -           * -           *      The custom `redirectTo` function is expected to return a string which will be used -           *      to update `$location.path()` and `$location.search()`. -           * -           *    - `[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 -           *              }); -           *            } -           * -           * @returns {Object} route object -           * -           * @description -           * Adds a new route definition to the `$route` service. -           */ -          when: function(path, route) { -            var routeDef = routes[path]; -            if (!routeDef) routeDef = routes[path] = {reloadOnSearch: true}; -            if (route) extend(routeDef, route); // TODO(im): what the heck? merge two route definitions? -            dirty++; -            return routeDef; -          }, - -          /** -           * @ngdoc method -           * @name angular.module.ng.$route#otherwise -           * @methodOf angular.module.ng.$route -           * -           * @description -           * Sets route definition that will be used on route change when no other route definition -           * is matched. -           * -           * @param {Object} params Mapping information to be assigned to `$route.current`. -           */ -          otherwise: function(params) { -            $route.when(null, params); -          }, - -          /** -           * @ngdoc method             * @name angular.module.ng.$route#reload             * @methodOf angular.module.ng.$route             *  | 
