Skip to content

Commit

Permalink
New segment config option default which can be set to true if this …
Browse files Browse the repository at this point in the history
…child segment should be loaded by default when no child is specified in the route.
  • Loading branch information
artch committed May 14, 2014
1 parent 2b255db commit 2eee0a1
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ app.config(function ($routeSegmentProvider) {

$routeSegmentProvider.

when('/section1', 's1.home').
when('/section1', 's1').
when('/section1/prefs', 's1.prefs').
when('/section1/:id', 's1.itemInfo.overview').
when('/section1/:id', 's1.itemInfo').
when('/section1/:id/edit', 's1.itemInfo.edit').
when('/section2', 's2').

Expand All @@ -55,6 +55,7 @@ $routeSegmentProvider.
within().

segment('home', {
default: true,
templateUrl: 'templates/section1/home.html'}).

segment('itemInfo', {
Expand All @@ -65,6 +66,7 @@ $routeSegmentProvider.
within().

segment('overview', {
default: true
templateUrl: 'templates/section1/item/overview.html'}).

segment('edit', {
Expand Down Expand Up @@ -186,7 +188,8 @@ Adds new segment at current pointer level.
- `watcher` is a $watch-function for recreating the view when its returning value is changed;
- `resolve` is a hash of functions or injectable names which should be resolved prior to instantiating the template and the controller;
- `untilResolved` is the alternate set of params (e.g. `template` and `controller`) which should be used before resolving is completed;
- `resolveFailed` is the alternate set of params which should be used if resolving failed.
- `resolveFailed` is the alternate set of params which should be used if resolving failed;
- `default` is a boolean value which can be set to true if this child segment should be loaded by default when no child is specified in the route.
##### within(childName)
Expand Down Expand Up @@ -246,7 +249,7 @@ A method for reverse routing which can return the route URL for the specified se
it is extended (overrided) with this provided object then.
```javascript
$routeSegment.getSegmentUrl('s1.home'); // -> '/section1'
$routeSegment.getSegmentUrl('s1'); // -> '/section1'
$routeSegment.getSegmentUrl('s1.prefs'); // -> '/section1/prefs'
$routeSegment.getSegmentUrl('s1.itemInfo', {id: 123}); // -> '/section1/123'
$routeSegment.getSegmentUrl('s1.itemInfo.edit', {id: 123}); // -> '/section1/123/edit'
Expand Down
46 changes: 39 additions & 7 deletions src/route-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ mod.provider( '$routeSegment',

var segmentName = route.segment;
var segmentNameChain = segmentName.split(".");
var updates = [];
var updates = [], lastUpdateIndex = -1;

for(var i=0; i < segmentNameChain.length; i++) {

Expand All @@ -233,8 +233,10 @@ mod.provider( '$routeSegment',
updates.length == 0 && !isDependenciesChanged(newSegment))
// if we went back to the same state as we were before resolving new segment
resolvingSemaphoreChain[i] = newSegment.name;
else
else {
updates.push({index: i, newSegment: newSegment});
lastUpdateIndex = i;
}
}
}

Expand All @@ -261,8 +263,6 @@ mod.provider( '$routeSegment',
updateSegment(j, null);
}
}


}
})
})(i);
Expand All @@ -276,10 +276,41 @@ mod.provider( '$routeSegment',
var oldLength = $routeSegment.chain.length;
var shortenBy = $routeSegment.chain.length - segmentNameChain.length;
$routeSegment.chain.splice(-shortenBy, shortenBy);
for(var i=segmentNameChain.length; i < oldLength; i++)
for(var i=segmentNameChain.length; i < oldLength; i++) {
updateSegment(i, null);
lastUpdateIndex = $routeSegment.chain.length-1;
}
}
})
}).then(function() {

var defaultChildUpdatePromise = $q.when();

if(lastUpdateIndex == $routeSegment.chain.length-1) {

var curSegment = getSegmentInChain(lastUpdateIndex, $routeSegment.name.split("."));

while(curSegment) {
var children = curSegment.children, index = lastUpdateIndex+1;
curSegment = null;
for (var i in children) {
(function(i, children, index) {
if (children[i].params.default) {
defaultChildUpdatePromise = defaultChildUpdatePromise.then(function () {
return updateSegment(index, {name: i, params: children[i].params})
.then(function (result) {
if (result.success) broadcast(result.success);
});
});
curSegment = children[i];
lastUpdateIndex = index;
}
})(i, children, index);
}
}
}

return defaultChildUpdatePromise;
});
}
});

Expand Down Expand Up @@ -434,7 +465,8 @@ mod.provider( '$routeSegment',

return {
name: nextName,
params: curSegment.params
params: curSegment.params,
children: curSegment.children
};
}

Expand Down
45 changes: 45 additions & 0 deletions test/unit/route-segment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,51 @@ describe('route segment', function() {
expect($routeSegment.chain.length).toBe(1);
})

describe('default=true', function() {
beforeEach(function () {
$routeSegmentProvider.when('/3', 'section3');
$routeSegmentProvider.when('/3/1', 'section3.section31');
$routeSegmentProvider.when('/3/1/1', 'section3.section31.section311');
$routeSegmentProvider.when('/3/1/2', 'section3.section31.section312');
$routeSegmentProvider.when('/3/2', 'section3.section32');
$routeSegmentProvider.segment('section3', {});
$routeSegmentProvider.within('section3').segment('section31', {default: true});
$routeSegmentProvider.within('section3').within('section31').segment('section311', {});
$routeSegmentProvider.within('section3').within('section31').segment('section312', {default: true});
$routeSegmentProvider.within('section3').segment('section32', {});
});

it('should auto-select a child with default=true', function () {
$location.path('/3');

$rootScope.$digest();
expect($routeSegment.name).toBe('section3.section31.section312');
expect($routeSegment.chain[0].name).toBe('section3');
expect($routeSegment.chain[1].name).toBe('section31');
expect($routeSegment.chain[2].name).toBe('section312');
expect(callback.calls[0].args[1].segment.name).toBe('section3');
expect(callback.calls[1].args[1].segment.name).toBe('section31');
expect(callback.calls[2].args[1].segment.name).toBe('section312');
});

it('should auto-select a child with default=true when coming from another child at the same level', function () {
$location.path('/3/2');
$rootScope.$digest();
$location.path('/3');

$rootScope.$digest();
expect($routeSegment.name).toBe('section3.section31.section312');
expect($routeSegment.chain[0].name).toBe('section3');
expect($routeSegment.chain[1].name).toBe('section31');
expect($routeSegment.chain[2].name).toBe('section312');
expect(callback.calls[0].args[1].segment.name).toBe('section3');
expect(callback.calls[1].args[1].segment.name).toBe('section32');
expect(callback.calls[2].args[1].segment).toBe(null);
expect(callback.calls[3].args[1].segment.name).toBe('section31');
expect(callback.calls[4].args[1].segment.name).toBe('section312');
})
});

describe('watcher', function() {

var watchedObj;
Expand Down

0 comments on commit 2eee0a1

Please sign in to comment.