aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMisko Hevery2010-04-15 14:17:33 -0700
committerMisko Hevery2010-04-15 14:17:33 -0700
commit70e401ef100614295fc808e32f0142f07c315461 (patch)
tree7d31580fb512dd535465e4d42afb0252b0cf0071 /src
parentcd03fe92a5dbd2aba516b64fc8067c5fba1e4a81 (diff)
downloadangular.js-70e401ef100614295fc808e32f0142f07c315461.tar.bz2
added $route service
Diffstat (limited to 'src')
-rw-r--r--src/AngularPublic.js1
-rw-r--r--src/Scope.js2
-rw-r--r--src/services.js40
-rw-r--r--src/widgets.js2
4 files changed, 44 insertions, 1 deletions
diff --git a/src/AngularPublic.js b/src/AngularPublic.js
index 176d6a91..1739ac4b 100644
--- a/src/AngularPublic.js
+++ b/src/AngularPublic.js
@@ -16,6 +16,7 @@ extend(angular, {
'extend': extend,
'foreach': foreach,
'noop':noop,
+ 'bind':bind,
'identity':identity,
'isUndefined': isUndefined,
'isDefined': isDefined,
diff --git a/src/Scope.js b/src/Scope.js
index 7529d726..8d44f4ef 100644
--- a/src/Scope.js
+++ b/src/Scope.js
@@ -117,6 +117,8 @@ function createScope(parent, services, existing) {
exceptionHandler(e);
} else if (exceptionHandler) {
errorHandlerFor(exceptionHandler, e);
+ } else if (isFunction(instance.$exceptionHandler)) {
+ instance.$exceptionHandler(e);
}
}
},
diff --git a/src/services.js b/src/services.js
index 11453338..90a5bb85 100644
--- a/src/services.js
+++ b/src/services.js
@@ -137,3 +137,43 @@ angularService("$invalidWidgets", function(){
}
return invalidWidgets;
});
+
+angularService('$route', function(location, params){
+ var routes = {},
+ onChange = [],
+ matcher = angularWidget('NG:SWITCH').route,
+ $route = {
+ routes: routes,
+ onChange: bind(onChange, onChange.push),
+ when:function (path, params){
+ if (angular.isUndefined(path)) return routes;
+ var route = routes[path];
+ if (!route) route = routes[path] = {};
+ if (params) angular.extend(route, params);
+ return route;
+ }
+ };
+ this.$watch(function(){return location.hash;}, function(hash){
+ var parentScope = this, childScope;
+ $route.current = null;
+ angular.foreach(routes, function(routeParams, route) {
+ if (!childScope) {
+ var pathParams = matcher(location.hashPath, route);
+ if (pathParams) {
+ childScope = angular.scope(parentScope);
+ $route.current = angular.extend({}, routeParams, {
+ scope: childScope,
+ params: angular.extend({}, location.hashSearch, pathParams)
+ });
+ }
+ }
+ });
+ angular.foreach(onChange, parentScope.$tryEval);
+ if (childScope) {
+ childScope.$become($route.current.controller);
+ parentScope.$tryEval(childScope.init);
+ }
+ });
+ return $route;
+}, {inject: ['$location']});
+
diff --git a/src/widgets.js b/src/widgets.js
index 28798c1b..09b602af 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -259,6 +259,6 @@ angularWidget('NG:SWITCH', function ngSwitch(element){
});
if (dstName) this.$set(dstName, dst);
}
- return match;
+ return match ? dst : null;
}
});