Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 70e401e

Browse files
author
Misko Hevery
committed
added $route service
1 parent cd03fe9 commit 70e401e

9 files changed

+140
-7
lines changed

angular-debug.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,8 @@ function createScope(parent, services, existing) {
807807
exceptionHandler(e);
808808
} else if (exceptionHandler) {
809809
errorHandlerFor(exceptionHandler, e);
810+
} else if (isFunction(instance.$exceptionHandler)) {
811+
instance.$exceptionHandler(e);
810812
}
811813
}
812814
},
@@ -3395,7 +3397,7 @@ angularWidget('NG:SWITCH', function ngSwitch(element){
33953397
});
33963398
if (dstName) this.$set(dstName, dst);
33973399
}
3398-
return match;
3400+
return match ? dst : null;
33993401
}
34003402
});
34013403
angularService("$window", bind(window, identity, window));

src/AngularPublic.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extend(angular, {
1616
'extend': extend,
1717
'foreach': foreach,
1818
'noop':noop,
19+
'bind':bind,
1920
'identity':identity,
2021
'isUndefined': isUndefined,
2122
'isDefined': isDefined,

src/Scope.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ function createScope(parent, services, existing) {
117117
exceptionHandler(e);
118118
} else if (exceptionHandler) {
119119
errorHandlerFor(exceptionHandler, e);
120+
} else if (isFunction(instance.$exceptionHandler)) {
121+
instance.$exceptionHandler(e);
120122
}
121123
}
122124
},

src/services.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,43 @@ angularService("$invalidWidgets", function(){
137137
}
138138
return invalidWidgets;
139139
});
140+
141+
angularService('$route', function(location, params){
142+
var routes = {},
143+
onChange = [],
144+
matcher = angularWidget('NG:SWITCH').route,
145+
$route = {
146+
routes: routes,
147+
onChange: bind(onChange, onChange.push),
148+
when:function (path, params){
149+
if (angular.isUndefined(path)) return routes;
150+
var route = routes[path];
151+
if (!route) route = routes[path] = {};
152+
if (params) angular.extend(route, params);
153+
return route;
154+
}
155+
};
156+
this.$watch(function(){return location.hash;}, function(hash){
157+
var parentScope = this, childScope;
158+
$route.current = null;
159+
angular.foreach(routes, function(routeParams, route) {
160+
if (!childScope) {
161+
var pathParams = matcher(location.hashPath, route);
162+
if (pathParams) {
163+
childScope = angular.scope(parentScope);
164+
$route.current = angular.extend({}, routeParams, {
165+
scope: childScope,
166+
params: angular.extend({}, location.hashSearch, pathParams)
167+
});
168+
}
169+
}
170+
});
171+
angular.foreach(onChange, parentScope.$tryEval);
172+
if (childScope) {
173+
childScope.$become($route.current.controller);
174+
parentScope.$tryEval(childScope.init);
175+
}
176+
});
177+
return $route;
178+
}, {inject: ['$location']});
179+

src/widgets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,6 @@ angularWidget('NG:SWITCH', function ngSwitch(element){
259259
});
260260
if (dstName) this.$set(dstName, dst);
261261
}
262-
return match;
262+
return match ? dst : null;
263263
}
264264
});

test/ScopeSpec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ describe('scope/model', function(){
8282
expect(element.hasClass('ng-exception')).toBeTruthy();
8383
});
8484

85+
it('should report error on $excetionHandler', function(){
86+
var element = jqLite('<div></div>');
87+
var scope = createScope();
88+
scope.$exceptionHandler = function(e){
89+
this.error = e;
90+
};
91+
scope.$tryEval('throw "myError"');
92+
expect(scope.error).toEqual("myError");
93+
});
94+
8595
// $onEval
8696

8797
it("should eval using priority", function(){

test/angular-mocks.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright (c) 2010 Adam Abrons and Misko Hevery http://getangular.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
124

225
function MockBrowser() {
326
var self = this, expectations = {}, requests = [];

test/servicesSpec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,41 @@ describe("service $invalidWidgets", function(){
9999
expect(scope.$invalidWidgets.length).toEqual(0);
100100
});
101101
});
102+
103+
describe("service $route", function(){
104+
it('should route and fire change event', function(){
105+
var log = '';
106+
function BookChapter() {
107+
this.log = '<init>';
108+
}
109+
BookChapter.prototype.init = function(){
110+
log += 'init();';
111+
};
112+
var scope = compile('<div></div>').$init();
113+
scope.$route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'});
114+
scope.$route.when('/Blank');
115+
scope.$route.onChange(function(){
116+
log += 'onChange();';
117+
});
118+
scope.$location.parse('http://server#/Book/Moby/Chapter/Intro?p=123');
119+
scope.$eval();
120+
expect(log).toEqual('onChange();init();');
121+
expect(scope.$route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
122+
expect(scope.$route.current.scope.log).toEqual('<init>');
123+
var lastId = scope.$route.current.scope.$id;
124+
125+
log = '';
126+
scope.$location.parse('http://server#/Blank?ignore');
127+
scope.$eval();
128+
expect(log).toEqual('onChange();');
129+
expect(scope.$route.current.params).toEqual({ignore:true});
130+
expect(scope.$route.current.scope.$id).not.toEqual(lastId);
131+
132+
log = '';
133+
scope.$location.parse('http://server#/NONE');
134+
scope.$eval();
135+
expect(log).toEqual('onChange();');
136+
expect(scope.$route.current).toEqual(null);
137+
138+
});
139+
});

test/testabilityPatch.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,29 @@ function sortedHtml(element) {
6464
}
6565
attrs.sort();
6666
html += attrs.join('');
67-
if (node.style && node.style.cssText) {
68-
var style = node.style.cssText.split('; ');
67+
if (node.style) {
68+
var style = [];
69+
if (node.style.cssText) {
70+
foreach(node.style.cssText.split(';'), function(value){
71+
value = trim(value);
72+
if (value) {
73+
style.push(value);
74+
}
75+
});
76+
}
77+
for(var css in node.style){
78+
var value = node.style[css];
79+
if (isString(value) && isString(css) && css != 'cssText' && value && (1*css != css)) {
80+
var text = css + ': ' + node.style[css];
81+
if (indexOf(style, text) == -1) {
82+
style.push(text);
83+
}
84+
}
85+
};
6986
style.sort();
70-
if (style[0] == '')
71-
style.shift();
72-
html += ' style="' + style.join('; ') + ';"';
87+
if (style.length) {
88+
html += ' style="' + style.join('; ') + ';"';
89+
}
7390
}
7491
html += '>';
7592
var children = node.childNodes;

0 commit comments

Comments
 (0)