Skip to content

Commit

Permalink
Fix a bug when inner segments are not reset properly while resolving …
Browse files Browse the repository at this point in the history
…the data
  • Loading branch information
artch committed Jan 26, 2014
1 parent 6e2a06e commit 510ab37
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 17 deletions.
37 changes: 28 additions & 9 deletions build/angular-route-segment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* angular-route-segment v1.2.0
* angular-route-segment
* https://angular-route-segment.com
* @author Artem Chivchalov
* @license MIT License http://opensource.org/licenses/MIT
Expand Down Expand Up @@ -205,10 +205,10 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',

var newSegment = getSegmentInChain( i, segmentNameChain );

if(resolvingSemaphoreChain[i] != newSegment.name || isDependenciesChanged(newSegment)) {
if(resolvingSemaphoreChain[i] != newSegment.name || updates.length > 0 || isDependenciesChanged(newSegment)) {

if($routeSegment.chain[i] && $routeSegment.chain[i].name == newSegment.name &&
!isDependenciesChanged(newSegment))
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
Expand All @@ -219,6 +219,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
var curSegmentPromise = $q.when();

if(updates.length > 0) {

for(var i=0; i<updates.length; i++) {
(function(i) {
curSegmentPromise = curSegmentPromise.then(function() {
Expand All @@ -228,13 +229,27 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
}).then(function(result) {

if(result.success != undefined) {

broadcast(result.success);

for(var j = updates[i].index + 1; j < $routeSegment.chain.length; j++) {

if($routeSegment.chain[j]) {
$routeSegment.chain[j] = null;
updateSegment(j, null);
}
}


}
})
})(i);
}

}



curSegmentPromise.then(function() {

// Removing redundant segment in case if new segment chain is shorter than old one
Expand Down Expand Up @@ -262,16 +277,19 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
});
return result;
}

function updateSegment(index, segment) {

function updateSegment(index, segment, isBroadcast) {

if(typeof isBroadcast === 'undefined')
isBroadcast = true;

if($routeSegment.chain[index] && $routeSegment.chain[index].clearWatcher) {
$routeSegment.chain[index].clearWatcher();
}

if(!segment) {
resolvingSemaphoreChain[index] = null;
broadcast(index);
isBroadcast && broadcast(index);
return;
}

Expand All @@ -281,7 +299,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
return resolve(index, segment.name, segment.params.untilResolved)
.then(function(result) {
if(result.success != undefined)
broadcast(index);
isBroadcast && broadcast(index);
return resolve(index, segment.name, segment.params);
})
}
Expand All @@ -306,7 +324,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
.then(function(response) {
return response.data;
});

return $q.all(locals).then(

function(resolvedLocals) {
Expand Down Expand Up @@ -371,7 +389,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',

$routeSegment.name = '';
for(var i=0; i<$routeSegment.chain.length; i++)
$routeSegment.name += $routeSegment.chain[i].name+".";
if($routeSegment.chain[i])
$routeSegment.name += $routeSegment.chain[i].name+".";
$routeSegment.name = $routeSegment.name.substr(0, $routeSegment.name.length-1);

$rootScope.$broadcast( 'routeSegmentChange', {
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.

22 changes: 19 additions & 3 deletions src/route-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
var curSegmentPromise = $q.when();

if(updates.length > 0) {

for(var i=0; i<updates.length; i++) {
(function(i) {
curSegmentPromise = curSegmentPromise.then(function() {
Expand All @@ -222,13 +223,27 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
}).then(function(result) {

if(result.success != undefined) {

broadcast(result.success);

for(var j = updates[i].index + 1; j < $routeSegment.chain.length; j++) {

if($routeSegment.chain[j]) {
$routeSegment.chain[j] = null;
updateSegment(j, null);
}
}


}
})
})(i);
}

}



curSegmentPromise.then(function() {

// Removing redundant segment in case if new segment chain is shorter than old one
Expand Down Expand Up @@ -256,7 +271,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
});
return result;
}

function updateSegment(index, segment) {

if($routeSegment.chain[index] && $routeSegment.chain[index].clearWatcher) {
Expand Down Expand Up @@ -300,7 +315,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
.then(function(response) {
return response.data;
});

return $q.all(locals).then(

function(resolvedLocals) {
Expand Down Expand Up @@ -365,7 +380,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',

$routeSegment.name = '';
for(var i=0; i<$routeSegment.chain.length; i++)
$routeSegment.name += $routeSegment.chain[i].name+".";
if($routeSegment.chain[i])
$routeSegment.name += $routeSegment.chain[i].name+".";
$routeSegment.name = $routeSegment.name.substr(0, $routeSegment.name.length-1);

$rootScope.$broadcast( 'routeSegmentChange', {
Expand Down
35 changes: 31 additions & 4 deletions test/unit/route-segment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,10 @@ describe('route segment', function() {

$rootScope.$digest();
expect($routeSegment.name).toBe('section-first');
expect(callback.calls.length).toBe(2);
//expect(callback.calls.length).toBe(2);
expect(callback.calls[0].args[1].index).toBe(0);
expect(callback.calls[0].args[1].segment.name).toBe('section-first');
expect(callback.calls[1].args[1].index).toBe(1);
expect(callback.calls[1].args[1].segment).toBe(null);
})

Expand Down Expand Up @@ -388,17 +390,19 @@ describe('route segment', function() {

describe('resolving', function() {

var resolve;
var resolve, resolveInner;

beforeEach(function() {
resolve = {};
$routeSegmentProvider.when('/3', 'section3');
resolve = {}, resolveInner = {};
$routeSegmentProvider.when('/3', 'section3');
$routeSegmentProvider.when('/3/1', 'section3.section31');
})

describe('without `untilResolved`', function() {

beforeEach(function() {
$routeSegmentProvider.segment('section3', {resolve: resolve});
$routeSegmentProvider.within('section3').segment('section31', {resolve: resolveInner});
})

it('should resolve a param as function', inject(function($q) {
Expand Down Expand Up @@ -520,10 +524,33 @@ describe('route segment', function() {

$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');
}))

it('should reset 2nd-level segment while loading when first-level is changed', inject(function($q) {

var defer = $q.defer();
resolveInner.param1 = function() { return defer.promise; };

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

$rootScope.$digest();
expect($routeSegment.name).toBe('section3');
expect($routeSegment.chain[1]).toBe(null);


defer.resolve();

$rootScope.$digest();
expect($routeSegment.name).toBe('section3.section31');
expect($routeSegment.chain.length).toBe(2);

}))
})

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

0 comments on commit 510ab37

Please sign in to comment.