diff options
| author | Igor Minar | 2011-01-30 16:57:57 -0800 | 
|---|---|---|
| committer | Igor Minar | 2011-02-01 09:33:59 -0800 | 
| commit | ce7ab3d1ee0e496c4b9838950b56fc1555b5bcf0 (patch) | |
| tree | 423cb842a146110c03f397f684d847c9b1e59fb4 | |
| parent | 7db3b54c1f777327a1213d7f7b761d238cdf9652 (diff) | |
| download | angular.js-ce7ab3d1ee0e496c4b9838950b56fc1555b5bcf0.tar.bz2 | |
add support for 404 handling via $route.otherwise
Closes #217
| -rw-r--r-- | src/services.js | 30 | ||||
| -rw-r--r-- | test/servicesSpec.js | 32 | 
2 files changed, 62 insertions, 0 deletions
| diff --git a/src/services.js b/src/services.js index 40f07b4d..15813add 100644 --- a/src/services.js +++ b/src/services.js @@ -702,6 +702,22 @@ angularServiceInject('$route', function(location) {            if (params) extend(route, params);            dirty++;            return route; +        }, + +        /** +         * @workInProgress +         * @ngdoc method +         * @name angular.service.$route#otherwise +         * @methodOf angular.service.$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);          }        };    function updateRoute(){ @@ -719,12 +735,26 @@ angularServiceInject('$route', function(location) {          }        }      }); + +    //fallback +    if (!childScope && routes[_null]) { +      childScope = createScope(parentScope); +      $route.current = extend({}, routes[_null], { +        scope: childScope, +        params: extend({}, location.hashSearch) +      }); +    } + +    //fire onChange callbacks      forEach(onChange, parentScope.$tryEval); +      if (childScope) {        childScope.$become($route.current.controller);      }    } +    this.$watch(function(){return dirty + location.hash;}, updateRoute); +    return $route;  }, ['$location']); diff --git a/test/servicesSpec.js b/test/servicesSpec.js index c4fadec8..02e874fe 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -411,6 +411,38 @@ describe("service", function(){        expect($route.current.controller).toBeUndefined();        expect(onChangeSpy).toHaveBeenCalled();      }); + +    it('should handle unknown routes with "otherwise" route definition', function() { +      var scope = angular.scope(), +          $location = scope.$service('$location'), +          $route = scope.$service('$route'), +          onChangeSpy = jasmine.createSpy('onChange'); + +      function NotFoundCtrl() {this.notFoundProp = 'not found!'} + +      $route.when('/foo', {template: 'foo.html'}); +      $route.otherwise({template: '404.html', controller: NotFoundCtrl}); +      $route.onChange(onChangeSpy); +      expect($route.current).toBeNull(); +      expect(onChangeSpy).not.toHaveBeenCalled(); + +      $location.updateHash('/unknownRoute'); +      scope.$eval(); + +      expect($route.current.template).toBe('404.html'); +      expect($route.current.controller).toBe(NotFoundCtrl); +      expect($route.current.scope.notFoundProp).toBe('not found!'); +      expect(onChangeSpy).toHaveBeenCalled(); + +      onChangeSpy.reset(); +      $location.updateHash('/foo'); +      scope.$eval(); + +      expect($route.current.template).toEqual('foo.html'); +      expect($route.current.controller).toBeUndefined(); +      expect($route.current.scope.notFoundProp).toBeUndefined(); +      expect(onChangeSpy).toHaveBeenCalled(); +    });    }); | 
