Skip to content

Commit

Permalink
fix a bug in resolving mechanics when another segment is selected in …
Browse files Browse the repository at this point in the history
…the meantime, fixes artch#4
  • Loading branch information
artch committed Aug 19, 2013
1 parent 6b24b49 commit 2054814
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 17 deletions.
26 changes: 18 additions & 8 deletions build/angular-route-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
},

/**
* Traverses to the root. *
* Traverses to the root.
* @returns The root pointer.
*/
root: function() {
Expand Down Expand Up @@ -181,7 +181,9 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
}
};

var lastParams = angular.copy($routeParams);
var lastParams = angular.copy($routeParams);

var resolvingSemaphoreChain = {};

// When a route changes, all interested parties should be notified about new segment chain
$rootScope.$on('$routeChangeSuccess', function(event, args) {
Expand All @@ -196,11 +198,13 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
for(var i=0; i < segmentNameChain.length; i++) {

var newSegment = getSegmentInChain( i, segmentNameChain );

if(!$routeSegment.chain[i] || $routeSegment.chain[i].name != newSegment.name ||
isDependenciesChanged(newSegment)) {

updates.push(updateSegment(i, newSegment));

if(resolvingSemaphoreChain[i] != newSegment.name || isDependenciesChanged(newSegment)) {
/*if($routeSegment.chain[i] && $routeSegment.chain[i].name == newSegment.name &&
!isDependenciesChanged(newSegment))
resolvingSemaphoreChain[i] = newSegment.name;
else */
updates.push(updateSegment(i, newSegment));
}
}

Expand Down Expand Up @@ -238,9 +242,12 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
}

if(!segment) {
resolvingSemaphoreChain[index] = null;
$rootScope.$broadcast( 'routeSegmentChange', { index: index, segment: null } );
return;
}

resolvingSemaphoreChain[index] = segment.name;

if(segment.params.untilResolved) {
return resolveAndBroadcast(index, segment.name, segment.params.untilResolved)
Expand Down Expand Up @@ -273,7 +280,10 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
return $q.all(locals).then(

function(resolvedLocals) {


if(resolvingSemaphoreChain[index] != name)
return $q.reject();

$routeSegment.chain[index] = {
name: name,
params: params,
Expand Down
2 changes: 1 addition & 1 deletion build/angular-route-segment.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ function MainCtrl($scope, $routeSegment, loader) {

$scope.$routeSegment = $routeSegment;
$scope.loader = loader;

$scope.$on('routeSegmentChange', function() {
loader.show = false;
})
}

function Section1Ctrl($scope, $routeSegment, $routeParams) {
Expand Down
26 changes: 18 additions & 8 deletions src/route-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
},

/**
* Traverses to the root. *
* Traverses to the root.
* @returns The root pointer.
*/
root: function() {
Expand Down Expand Up @@ -175,7 +175,9 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
}
};

var lastParams = angular.copy($routeParams);
var lastParams = angular.copy($routeParams);

var resolvingSemaphoreChain = {};

// When a route changes, all interested parties should be notified about new segment chain
$rootScope.$on('$routeChangeSuccess', function(event, args) {
Expand All @@ -190,11 +192,13 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
for(var i=0; i < segmentNameChain.length; i++) {

var newSegment = getSegmentInChain( i, segmentNameChain );

if(!$routeSegment.chain[i] || $routeSegment.chain[i].name != newSegment.name ||
isDependenciesChanged(newSegment)) {

updates.push(updateSegment(i, newSegment));

if(resolvingSemaphoreChain[i] != newSegment.name || isDependenciesChanged(newSegment)) {
if($routeSegment.chain[i] && $routeSegment.chain[i].name == newSegment.name &&
!isDependenciesChanged(newSegment))
resolvingSemaphoreChain[i] = newSegment.name;
else
updates.push(updateSegment(i, newSegment));
}
}

Expand Down Expand Up @@ -232,9 +236,12 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
}

if(!segment) {
resolvingSemaphoreChain[index] = null;
$rootScope.$broadcast( 'routeSegmentChange', { index: index, segment: null } );
return;
}

resolvingSemaphoreChain[index] = segment.name;

if(segment.params.untilResolved) {
return resolveAndBroadcast(index, segment.name, segment.params.untilResolved)
Expand Down Expand Up @@ -267,7 +274,10 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
return $q.all(locals).then(

function(resolvedLocals) {


if(resolvingSemaphoreChain[index] != name)
return $q.reject();

$routeSegment.chain[index] = {
name: name,
params: params,
Expand Down
47 changes: 47 additions & 0 deletions test/unit/route-segment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,53 @@ describe('route segment', function() {
$rootScope.$digest();
expect($routeSegment.name).not.toEqual('section3');
}))

it('should not restore previous yet unresolved segment after new one is selected', inject(function($q) {

var defer = $q.defer();
resolve.param1 = function() { return defer.promise; };
$location.path('/3');
$rootScope.$digest();

$location.path('/1');

$rootScope.$digest();
expect($routeSegment.name).toEqual('section-first');

defer.resolve();

$rootScope.$digest();
expect(callback.calls.length).toBe(1);
expect($routeSegment.chain[0].name).toEqual('section-first');
expect($routeSegment.name).toEqual('section-first');
}))

it('should not restore previous yet unresolved segment after new one is selected in 2nd level',
inject(function($q) {

$location.path('/2/X');
$rootScope.$digest();

callback = jasmine.createSpy('event');
$rootScope.$on('routeSegmentChange', callback);
var defer = $q.defer();
resolve.param1 = function() { return defer.promise; };
$location.path('/3');
$rootScope.$digest();

$location.path('/2/X');

$rootScope.$digest();
expect($routeSegment.name).toEqual('section2.section21');

defer.resolve();

$rootScope.$digest();
expect(callback).not.toHaveBeenCalled();
expect($routeSegment.chain[0].name).toEqual('section2');
expect($routeSegment.chain[1].name).toEqual('section21');
expect($routeSegment.name).toEqual('section2.section21');
}))
})

describe('with `untilResolved`', function() {
Expand Down

0 comments on commit 2054814

Please sign in to comment.