Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb27c91

Browse files
committed
MERGE: scope
1 parent 67128c2 commit bb27c91

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

src/service/route.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,31 +201,32 @@ angularServiceInject('$route', function($location, $updateView) {
201201

202202

203203
function updateRoute(){
204-
var childScope, routeParams, pathParams, segmentMatch, key, redir;
204+
var selectedRoute, pathParams, segmentMatch, key, redir;
205205

206-
if ($route.current) {
206+
if ($route.current && $route.current.scope) {
207207
$route.current.scope.$destroy();
208208
}
209209
$route.current = null;
210+
// Match a route
210211
forEach(routes, function(rParams, rPath) {
211212
if (!pathParams) {
212213
if (pathParams = matcher($location.hashPath, rPath)) {
213-
routeParams = rParams;
214+
selectedRoute = rParams;
214215
}
215216
}
216217
});
217218

218-
// "otherwise" fallback
219-
routeParams = routeParams || routes[null];
219+
// No route matched; fallback to "otherwise" route
220+
selectedRoute = selectedRoute || routes[null];
220221

221-
if(routeParams) {
222-
if (routeParams.redirectTo) {
223-
if (isString(routeParams.redirectTo)) {
222+
if(selectedRoute) {
223+
if (selectedRoute.redirectTo) {
224+
if (isString(selectedRoute.redirectTo)) {
224225
// interpolate the redirectTo string
225226
redir = {hashPath: '',
226227
hashSearch: extend({}, $location.hashSearch, pathParams)};
227228

228-
forEach(routeParams.redirectTo.split(':'), function(segment, i) {
229+
forEach(selectedRoute.redirectTo.split(':'), function(segment, i) {
229230
if (i==0) {
230231
redir.hashPath += segment;
231232
} else {
@@ -238,23 +239,25 @@ angularServiceInject('$route', function($location, $updateView) {
238239
});
239240
} else {
240241
// call custom redirectTo function
241-
redir = {hash: routeParams.redirectTo(pathParams, $location.hash, $location.hashPath,
242+
redir = {hash: selectedRoute.redirectTo(pathParams, $location.hash, $location.hashPath,
242243
$location.hashSearch)};
243244
}
244245

245246
$location.update(redir);
246247
return;
247248
}
248249

249-
childScope = parentScope.$new(routeParams.controller);
250-
$route.current = extend({}, routeParams, {
251-
scope: childScope,
252-
params: extend({}, $location.hashSearch, pathParams)
253-
});
250+
$route.current = extend({}, selectedRoute);
251+
$route.current.params = extend({}, $location.hashSearch, pathParams);
254252
}
255253

256254
//fire onChange callbacks
257255
forEach(onChange, parentScope.$eval, parentScope);
256+
257+
// Create the scope if we have mtched a route
258+
if ($route.current) {
259+
$route.current.scope = parentScope.$new($route.current.controller);
260+
}
258261
}
259262

260263

src/widgets.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,27 +1228,21 @@ angularWidget('ng:view', function(element) {
12281228
if (!element[0]['ng:compiled']) {
12291229
element[0]['ng:compiled'] = true;
12301230
return annotate('$xhr.cache', '$route', function($xhr, $route, element){
1231-
var childScope;
1232-
var src;
1231+
var template;
12331232
var changeCounter = 0;
12341233

12351234
$route.onChange(function(){
1236-
if ($route.current) {
1237-
src = $route.current.template;
1238-
childScope = $route.current.scope;
1239-
} else {
1240-
src = childScope = null;
1241-
}
12421235
changeCounter++;
12431236
})(); //initialize the state forcefully, it's possible that we missed the initial
12441237
//$route#onChange already
12451238

12461239
this.$observe(function(){return changeCounter;}, function(){
1247-
if (src) {
1240+
var template = $route.current && $route.current.template;
1241+
if (template) {
12481242
//xhr's callback must be async, see commit history for more info
1249-
$xhr('GET', src, function(code, response){
1243+
$xhr('GET', template, function(code, response){
12501244
element.html(response);
1251-
compiler.compile(element)(childScope);
1245+
compiler.compile(element)($route.current.scope);
12521246
});
12531247
} else {
12541248
element.html('');

test/service/routeSpec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('$route', function() {
1616
$location, $route;
1717

1818
function BookChapter() {
19-
this.log = '<init>';
19+
log += '<init>';
2020
}
2121
scope = compile('<div></div>')();
2222
$location = scope.$service('$location');
@@ -26,11 +26,11 @@ describe('$route', function() {
2626
$route.onChange(function(){
2727
log += 'onChange();';
2828
});
29+
2930
$location.update('http://server#/Book/Moby/Chapter/Intro?p=123');
3031
scope.$digest();
31-
expect(log).toEqual('onChange();');
32+
expect(log).toEqual('onChange();<init>');
3233
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
33-
expect($route.current.scope.log).toEqual('<init>');
3434
var lastId = $route.current.scope.$id;
3535

3636
log = '';

0 commit comments

Comments
 (0)