Skip to content

Commit eb0d8a0

Browse files
committed
fix a double update of a view segment by $timeout
1 parent 3c66211 commit eb0d8a0

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

Gruntfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = function(grunt) {
66

77
uglify: {
88
options: {
9-
banner: "/**\n * angular-route-segment <%=grunt.config('gitdescribe')[1]%>\n * https://angular-route-segment.com\n * @author Artem Chivchalov\n * @license MIT License http://opensource.org/licenses/MIT\n */\n"
9+
banner: "/**\n * angular-route-segment <%=pkg.version%>\n * https://angular-route-segment.com\n * @author Artem Chivchalov\n * @license MIT License http://opensource.org/licenses/MIT\n */\n"
1010
},
1111
prod: {
1212
files: {
@@ -18,7 +18,7 @@ module.exports = function(grunt) {
1818
concat: {
1919
options: {
2020
separator: ';',
21-
banner: "/**\n * angular-route-segment <%=(grunt.config('gitdescribe') && grunt.config('gitdescribe')[1])%>\n * https://angular-route-segment.com\n * @author Artem Chivchalov\n * @license MIT License http://opensource.org/licenses/MIT\n */\n"
21+
banner: "/**\n * angular-route-segment <%=pkg.version%>\n * https://angular-route-segment.com\n * @author Artem Chivchalov\n * @license MIT License http://opensource.org/licenses/MIT\n */\n"
2222
},
2323
prod: {
2424
src: ['src/**/*.js'],

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-route-segment",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"main": "build/angular-route-segment.js",
55
"ignore": [
66
"**/.*",
@@ -14,6 +14,6 @@
1414
"karma-angular-1.2.0rc1.conf.js",
1515
"package.json",
1616
"src",
17-
"build/angular-route-segment.min.js"
17+
"**/*.min.js"
1818
]
1919
}

build/angular-route-segment.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* angular-route-segment v1.2.0
2+
* angular-route-segment 1.2.3
33
* https://angular-route-segment.com
44
* @author Artem Chivchalov
55
* @license MIT License http://opensource.org/licenses/MIT
@@ -453,7 +453,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
453453
return function($scope) {
454454

455455
var currentScope, currentElement, currentSegment, onloadExp = tAttrs.onload || '', animate,
456-
viewSegmentIndex = parseInt(tAttrs.appViewSegment);
456+
viewSegmentIndex = parseInt(tAttrs.appViewSegment), updatePromise;
457457

458458
try {
459459
// angular 1.1.x
@@ -468,13 +468,16 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
468468
catch(e) {}
469469

470470
if($routeSegment.chain[viewSegmentIndex])
471-
$timeout(function() {
471+
updatePromise = $timeout(function() {
472472
update($routeSegment.chain[viewSegmentIndex]);
473473
}, 0);
474474

475475
// Watching for the specified route segment and updating contents
476476
$scope.$on('routeSegmentChange', function(event, args) {
477477

478+
if(updatePromise)
479+
$timeout.cancel(updatePromise);
480+
478481
if(args.index == viewSegmentIndex && currentSegment != args.segment)
479482
update(args.segment);
480483
});

build/angular-route-segment.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-route-segment",
3-
"version": "1.0.0",
3+
"version": "1.2.3",
44
"dependencies": {
55
"grunt": "",
66
"grunt-contrib-uglify": "",

src/view-segment.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
return function($scope) {
2626

2727
var currentScope, currentElement, currentSegment, onloadExp = tAttrs.onload || '', animate,
28-
viewSegmentIndex = parseInt(tAttrs.appViewSegment);
28+
viewSegmentIndex = parseInt(tAttrs.appViewSegment), updatePromise;
2929

3030
try {
3131
// angular 1.1.x
@@ -40,13 +40,16 @@
4040
catch(e) {}
4141

4242
if($routeSegment.chain[viewSegmentIndex])
43-
$timeout(function() {
43+
updatePromise = $timeout(function() {
4444
update($routeSegment.chain[viewSegmentIndex]);
4545
}, 0);
4646

4747
// Watching for the specified route segment and updating contents
4848
$scope.$on('routeSegmentChange', function(event, args) {
4949

50+
if(updatePromise)
51+
$timeout.cancel(updatePromise);
52+
5053
if(args.index == viewSegmentIndex && currentSegment != args.segment)
5154
update(args.segment);
5255
});

test/unit/view-segment.spec.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ describe('view-segment', function() {
256256
expect(spy.calls.length).toBe(2);
257257
}))
258258

259-
it('should not call the controller of a sub-segment called previously', function() {
259+
it('should not call the controller of a sub-segment called previously if new segment doesnt have a sub-segment', function() {
260260

261261
var spy = jasmine.createSpy('controller');
262262

@@ -280,6 +280,31 @@ describe('view-segment', function() {
280280

281281
})
282282

283+
it('should not call the controller of a sub-segment called previously if new segment has a sub-segment', inject(function($timeout) {
284+
285+
var spy = jasmine.createSpy('controller');
286+
287+
$routeSegmentProvider.when('/3/1', 'section3.section31');
288+
$routeSegmentProvider.segment('section3', {
289+
template: '<div app:view-segment="1"></div>'
290+
}).within().segment('section31', {
291+
template: '<div></div>',
292+
controller: spy
293+
})
294+
295+
$location.path('/2/1');
296+
$rootScope.$digest();
297+
$location.path('/3/1');
298+
299+
$rootScope.$digest();
300+
try {
301+
$timeout.flush();
302+
}
303+
catch(e) {}
304+
expect(spy.calls.length).toBe(1);
305+
306+
}))
307+
283308
describe('a view with empty template', function() {
284309

285310
beforeEach(function() {

0 commit comments

Comments
 (0)