Skip to content

Commit

Permalink
fix a double update of a view segment by $timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
artch committed Apr 7, 2014
1 parent 3c66211 commit eb0d8a0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function(grunt) {

uglify: {
options: {
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"
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"
},
prod: {
files: {
Expand All @@ -18,7 +18,7 @@ module.exports = function(grunt) {
concat: {
options: {
separator: ';',
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"
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"
},
prod: {
src: ['src/**/*.js'],
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-route-segment",
"version": "1.2.2",
"version": "1.2.3",
"main": "build/angular-route-segment.js",
"ignore": [
"**/.*",
Expand All @@ -14,6 +14,6 @@
"karma-angular-1.2.0rc1.conf.js",
"package.json",
"src",
"build/angular-route-segment.min.js"
"**/*.min.js"
]
}
9 changes: 6 additions & 3 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 1.2.3
* https://angular-route-segment.com
* @author Artem Chivchalov
* @license MIT License http://opensource.org/licenses/MIT
Expand Down Expand Up @@ -453,7 +453,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
return function($scope) {

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

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

if($routeSegment.chain[viewSegmentIndex])
$timeout(function() {
updatePromise = $timeout(function() {
update($routeSegment.chain[viewSegmentIndex]);
}, 0);

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

if(updatePromise)
$timeout.cancel(updatePromise);

if(args.index == viewSegmentIndex && currentSegment != args.segment)
update(args.segment);
});
Expand Down
4 changes: 2 additions & 2 deletions build/angular-route-segment.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-route-segment",
"version": "1.0.0",
"version": "1.2.3",
"dependencies": {
"grunt": "",
"grunt-contrib-uglify": "",
Expand Down
7 changes: 5 additions & 2 deletions src/view-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
return function($scope) {

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

try {
// angular 1.1.x
Expand All @@ -40,13 +40,16 @@
catch(e) {}

if($routeSegment.chain[viewSegmentIndex])
$timeout(function() {
updatePromise = $timeout(function() {
update($routeSegment.chain[viewSegmentIndex]);
}, 0);

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

if(updatePromise)
$timeout.cancel(updatePromise);

if(args.index == viewSegmentIndex && currentSegment != args.segment)
update(args.segment);
});
Expand Down
27 changes: 26 additions & 1 deletion test/unit/view-segment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ describe('view-segment', function() {
expect(spy.calls.length).toBe(2);
}))

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

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

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

})

it('should not call the controller of a sub-segment called previously if new segment has a sub-segment', inject(function($timeout) {

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

$routeSegmentProvider.when('/3/1', 'section3.section31');
$routeSegmentProvider.segment('section3', {
template: '<div app:view-segment="1"></div>'
}).within().segment('section31', {
template: '<div></div>',
controller: spy
})

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

$rootScope.$digest();
try {
$timeout.flush();
}
catch(e) {}
expect(spy.calls.length).toBe(1);

}))

describe('a view with empty template', function() {

beforeEach(function() {
Expand Down

0 comments on commit eb0d8a0

Please sign in to comment.