@@ -361,7 +361,7 @@ angularWidget('@ng:repeat', function(expression, element){
361
361
// We expect this to be a rare case.
362
362
var lastOrder = new HashQueueMap ( ) ;
363
363
this . $watch ( function ( scope ) {
364
- var index = 0 ,
364
+ var index , length ,
365
365
collection = scope . $eval ( rhs ) ,
366
366
collectionLength = size ( collection , true ) ,
367
367
childScope ,
@@ -372,52 +372,64 @@ angularWidget('@ng:repeat', function(expression, element){
372
372
array , last , // last object information {scope, element, index}
373
373
cursor = iterStartElement ; // current position of the node
374
374
375
- for ( key in collection ) {
376
- if ( collection . hasOwnProperty ( key ) && key . charAt ( 0 ) != '$' ) {
377
- last = lastOrder . shift ( value = collection [ key ] ) ;
378
- if ( last ) {
379
- // if we have already seen this object, then we need to reuse the
380
- // associated scope/element
381
- childScope = last . scope ;
382
- nextOrder . push ( value , last ) ;
383
-
384
- if ( index === last . index ) {
385
- // do nothing
386
- cursor = last . element ;
387
- } else {
388
- // existing item which got moved
389
- last . index = index ;
390
- // This may be a noop, if the element is next, but I don't know of a good way to
391
- // figure this out, since it would require extra DOM access, so let's just hope that
392
- // the browsers realizes that it is noop, and treats it as such.
393
- cursor . after ( last . element ) ;
394
- cursor = last . element ;
395
- }
396
- } else {
397
- // new item which we don't know about
398
- childScope = parentScope . $new ( ) ;
375
+ if ( ! isArray ( collection ) ) {
376
+ // if object, extract keys, sort them and use to determine order of iteration over obj props
377
+ array = [ ] ;
378
+ for ( key in collection ) {
379
+ if ( collection . hasOwnProperty ( key ) && key . charAt ( 0 ) != '$' ) {
380
+ array . push ( key ) ;
399
381
}
382
+ }
383
+ array . sort ( ) ;
384
+ } else {
385
+ array = collection || [ ] ;
386
+ }
400
387
401
- childScope [ valueIdent ] = collection [ key ] ;
402
- if ( keyIdent ) childScope [ keyIdent ] = key ;
403
- childScope . $index = index ;
404
- childScope . $position = index == 0
405
- ? 'first'
406
- : ( index == collectionLength - 1 ? 'last' : 'middle' ) ;
407
-
408
- if ( ! last ) {
409
- linker ( childScope , function ( clone ) {
410
- cursor . after ( clone ) ;
411
- last = {
412
- scope : childScope ,
413
- element : ( cursor = clone ) ,
414
- index : index
415
- } ;
416
- nextOrder . push ( value , last ) ;
417
- } ) ;
388
+ // we are not using forEach for perf reasons (trying to avoid #call)
389
+ for ( index = 0 , length = array . length ; index < length ; index ++ ) {
390
+ key = ( collection === array ) ? index : array [ index ] ;
391
+ value = collection [ key ] ;
392
+ last = lastOrder . shift ( value ) ;
393
+ if ( last ) {
394
+ // if we have already seen this object, then we need to reuse the
395
+ // associated scope/element
396
+ childScope = last . scope ;
397
+ nextOrder . push ( value , last ) ;
398
+
399
+ if ( index === last . index ) {
400
+ // do nothing
401
+ cursor = last . element ;
402
+ } else {
403
+ // existing item which got moved
404
+ last . index = index ;
405
+ // This may be a noop, if the element is next, but I don't know of a good way to
406
+ // figure this out, since it would require extra DOM access, so let's just hope that
407
+ // the browsers realizes that it is noop, and treats it as such.
408
+ cursor . after ( last . element ) ;
409
+ cursor = last . element ;
418
410
}
411
+ } else {
412
+ // new item which we don't know about
413
+ childScope = parentScope . $new ( ) ;
414
+ }
419
415
420
- index ++ ;
416
+ childScope [ valueIdent ] = value ;
417
+ if ( keyIdent ) childScope [ keyIdent ] = key ;
418
+ childScope . $index = index ;
419
+ childScope . $position = index == 0
420
+ ? 'first'
421
+ : ( index == collectionLength - 1 ? 'last' : 'middle' ) ;
422
+
423
+ if ( ! last ) {
424
+ linker ( childScope , function ( clone ) {
425
+ cursor . after ( clone ) ;
426
+ last = {
427
+ scope : childScope ,
428
+ element : ( cursor = clone ) ,
429
+ index : index
430
+ } ;
431
+ nextOrder . push ( value , last ) ;
432
+ } ) ;
421
433
}
422
434
}
423
435
0 commit comments