1
1
/**
2
- * angular-route-segment v1.1 .0
2
+ * angular-route-segment v1.2 .0
3
3
* https://angular-route-segment.com
4
4
* @author Artem Chivchalov
5
5
* @license MIT License http://opensource.org/licenses/MIT
@@ -212,19 +212,30 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
212
212
// if we went back to the same state as we were before resolving new segment
213
213
resolvingSemaphoreChain [ i ] = newSegment . name ;
214
214
else
215
- updates . push ( updateSegment ( i , newSegment ) ) ;
215
+ updates . push ( { index : i , newSegment : newSegment } ) ;
216
216
}
217
217
}
218
218
219
- $q . all ( updates ) . then ( function ( result ) {
219
+ var curSegmentPromise = $q . when ( ) ;
220
220
221
- $routeSegment . name = segmentName ;
222
- $routeSegment . $routeParams = angular . copy ( $routeParams ) ;
221
+ if ( updates . length > 0 ) {
222
+ for ( var i = 0 ; i < updates . length ; i ++ ) {
223
+ ( function ( i ) {
224
+ curSegmentPromise = curSegmentPromise . then ( function ( ) {
223
225
224
- for ( var i = 0 ; i < result . length ; i ++ ) {
225
- if ( result [ i ] . success != undefined )
226
- broadcast ( result [ i ] . success ) ;
226
+ return updateSegment ( updates [ i ] . index , updates [ i ] . newSegment ) ;
227
+
228
+ } ) . then ( function ( result ) {
229
+
230
+ if ( result . success != undefined ) {
231
+ broadcast ( result . success ) ;
232
+ }
233
+ } )
234
+ } ) ( i ) ;
227
235
}
236
+ }
237
+
238
+ curSegmentPromise . then ( function ( ) {
228
239
229
240
// Removing redundant segment in case if new segment chain is shorter than old one
230
241
if ( $routeSegment . chain . length > segmentNameChain . length ) {
@@ -234,7 +245,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
234
245
for ( var i = segmentNameChain . length ; i < oldLength ; i ++ )
235
246
updateSegment ( i , null ) ;
236
247
}
237
- } ) ;
248
+ } )
249
+
238
250
239
251
240
252
}
@@ -264,7 +276,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
264
276
}
265
277
266
278
resolvingSemaphoreChain [ index ] = segment . name ;
267
-
279
+
268
280
if ( segment . params . untilResolved ) {
269
281
return resolve ( index , segment . name , segment . params . untilResolved )
270
282
. then ( function ( result ) {
@@ -354,6 +366,14 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
354
366
}
355
367
356
368
function broadcast ( index ) {
369
+
370
+ $routeSegment . $routeParams = angular . copy ( $routeParams ) ;
371
+
372
+ $routeSegment . name = '' ;
373
+ for ( var i = 0 ; i < $routeSegment . chain . length ; i ++ )
374
+ $routeSegment . name += $routeSegment . chain [ i ] . name + "." ;
375
+ $routeSegment . name = $routeSegment . name . substr ( 0 , $routeSegment . name . length - 1 ) ;
376
+
357
377
$rootScope . $broadcast ( 'routeSegmentChange' , {
358
378
index : index ,
359
379
segment : $routeSegment . chain [ index ] || null } ) ;
@@ -393,20 +413,9 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
393
413
} ) ( angular ) ; ; 'use strict' ;
394
414
395
415
/**
396
- * The directive app:view is more powerful replacement of built-in ng:view. It allows views to be nested, where each
397
- * following view in the chain corresponds to the next route segment (see $routeSegment service).
398
- *
399
- * Sample:
400
- * <div>
401
- * <h4>Section</h4>
402
- * <div app:view>Nothing selected</div>
403
- * </div>
404
- *
405
- * Initial contents of an element with app:view will display if corresponding route segment doesn't exist.
406
- *
407
- * View resolving are depends on route segment params:
408
- * - `template` define contents of the view
409
- * - `controller` is attached to view contents when compiled and linked
416
+ * appViewSegment directive
417
+ * It is based on ngView directive code:
418
+ * https://github.com/angular/angular.js/blob/master/src/ngRoute/directive/ngView.js
410
419
*/
411
420
412
421
( function ( angular ) {
@@ -427,7 +436,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
427
436
428
437
return function ( $scope ) {
429
438
430
- var currentScope , currentElement , onloadExp = tAttrs . onload || '' , animate ,
439
+ var currentScope , currentElement , currentSegment , onloadExp = tAttrs . onload || '' , animate ,
431
440
viewSegmentIndex = parseInt ( tAttrs . appViewSegment ) ;
432
441
433
442
try {
@@ -447,7 +456,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
447
456
448
457
// Watching for the specified route segment and updating contents
449
458
$scope . $on ( 'routeSegmentChange' , function ( event , args ) {
450
- if ( args . index == viewSegmentIndex )
459
+ if ( args . index == viewSegmentIndex && currentSegment != args . segment )
451
460
update ( args . segment ) ;
452
461
} ) ;
453
462
@@ -467,6 +476,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
467
476
468
477
function update ( segment ) {
469
478
479
+ currentSegment = segment ;
480
+
470
481
if ( isDefault ) {
471
482
isDefault = false ;
472
483
tElement . replaceWith ( anchor ) ;
@@ -484,36 +495,31 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
484
495
var locals = angular . extend ( { } , segment . locals ) ,
485
496
template = locals && locals . $template ;
486
497
487
- if ( template ) {
488
-
489
- clearContent ( ) ;
490
-
491
- currentElement = tElement . clone ( ) ;
492
- currentElement . html ( template ) ;
493
- animate . enter ( currentElement , null , anchor ) ;
498
+ clearContent ( ) ;
494
499
495
- var link = $compile ( currentElement , false , 499 ) , controller ;
500
+ currentElement = tElement . clone ( ) ;
501
+ currentElement . html ( template ? template : defaultContent ) ;
502
+ animate . enter ( currentElement , null , anchor ) ;
496
503
497
- currentScope = $scope . $new ( ) ;
498
- if ( segment . params . controller ) {
499
- locals . $scope = currentScope ;
500
- controller = $controller ( segment . params . controller , locals ) ;
501
- if ( segment . params . controllerAs )
502
- currentScope [ segment . params . controllerAs ] = controller ;
503
- currentElement . data ( '$ngControllerController' , controller ) ;
504
- currentElement . children ( ) . data ( '$ngControllerController' , controller ) ;
505
- }
504
+ var link = $compile ( currentElement , false , 499 ) , controller ;
506
505
507
- link ( currentScope ) ;
508
- currentScope . $emit ( '$viewContentLoaded' ) ;
509
- currentScope . $eval ( onloadExp ) ;
510
- } else {
511
- clearContent ( ) ;
506
+ currentScope = $scope . $new ( ) ;
507
+ if ( segment . params . controller ) {
508
+ locals . $scope = currentScope ;
509
+ controller = $controller ( segment . params . controller , locals ) ;
510
+ if ( segment . params . controllerAs )
511
+ currentScope [ segment . params . controllerAs ] = controller ;
512
+ currentElement . data ( '$ngControllerController' , controller ) ;
513
+ currentElement . children ( ) . data ( '$ngControllerController' , controller ) ;
512
514
}
515
+
516
+ link ( currentScope ) ;
517
+ currentScope . $emit ( '$viewContentLoaded' ) ;
518
+ currentScope . $eval ( onloadExp ) ;
513
519
}
514
520
}
515
521
}
516
522
}
517
523
} ] ) ;
518
524
519
- } ) ( angular ) ;
525
+ } ) ( angular ) ;
0 commit comments