22*
33* ModelView.js
44* @version : 4.0.6
5- * @built on 2022-03-07 20:29:32
5+ * @built on 2022-03-08 10:08:22
66*
77* A simple, light-weight, versatile and fast isomorphic MVVM JavaScript framework (Browser and Server)
88* https://github.com/foo123/modelview.js
1111*
1212* ModelView.js
1313* @version : 4.0.6
14- * @built on 2022-03-07 20:29:32
14+ * @built on 2022-03-08 10:08:22
1515*
1616* A simple, light-weight, versatile and fast isomorphic MVVM JavaScript framework (Browser and Server)
1717* https://github.com/foo123/modelview.js
@@ -2123,7 +2123,7 @@ var
21232123 } ,
21242124 morphSelectedNodes = function morphSelectedNodes ( view , r , v , start , end , end2 , startv , count ) {
21252125 var index , indexv , vnode , rnode , T , collection ,
2126- diff , di , dc , d , items , nodes ,
2126+ diff , di , dc , d , items , keyed ,
21272127 i , j , k , l , m , n , w , x , z , len , frag ;
21282128 if ( 'collection' === v . childNodes [ startv ] . nodeType )
21292129 {
@@ -2204,14 +2204,19 @@ var
22042204 return count ;
22052205 }
22062206
2207- for ( nodes = { } , index = start ; index <= end ; index ++ )
2207+ keyed = { } ;
2208+ if ( v . childNodes [ startv ] . id || v . childNodes [ stdMath . min ( v . childNodes . length - 1 , startv + end - start ) ] . id )
22082209 {
2209- if ( index >= r . childNodes . length ) break ;
2210- rnode = r . childNodes [ index ] ;
2211- rnode [ MV ] = rnode [ MV ] || MV0 ( ) ;
2212- // store the keyed nodes in a map
2213- // to be retrieved easily below
2214- if ( rnode [ MV ] . id ) nodes [ '#' + rnode [ MV ] . id ] = rnode ;
2210+ // there are keyed nodes, associate them in a map for reuse
2211+ for ( index = start ; index <= end ; index ++ )
2212+ {
2213+ if ( index >= r . childNodes . length ) break ;
2214+ rnode = r . childNodes [ index ] ;
2215+ rnode [ MV ] = rnode [ MV ] || MV0 ( ) ;
2216+ // store the keyed nodes in a map
2217+ // to be retrieved and reused easily
2218+ if ( rnode [ MV ] . id ) keyed [ '#' + rnode [ MV ] . id ] = rnode ;
2219+ }
22152220 }
22162221 for ( indexv = startv , index = start ; index <= end ; index ++ , indexv ++ )
22172222 {
@@ -2237,9 +2242,9 @@ var
22372242 {
22382243 morphSingle ( view , r , rnode , vnode ) ;
22392244 }
2240- else if ( vnode . id && ( frag = nodes [ '#' + vnode . id ] ) && eqNodes ( frag , vnode ) )
2245+ else if ( vnode . id && ( frag = keyed [ '#' + vnode . id ] ) && eqNodes ( frag , vnode ) )
22412246 {
2242- r . replaceChild ( frag , rnode ) ;
2247+ r . insertBefore ( frag , rnode ) ;
22432248 morphSingle ( view , r , frag , vnode ) ;
22442249 }
22452250 else if ( 0 === count )
@@ -2298,7 +2303,7 @@ var
22982303 // morph r (real) DOM to match v (virtual) DOM
22992304 var vc = v . childNodes . length , vpc = v . potentialChildNodes ,
23002305 count = 0 , offset = 0 , matched , match ,
2301- mi , m , mc , di , dc , i , j , index , nodes ,
2306+ mi , m , mc , di , dc , i , j , index , keyed ,
23022307 vnode , rnode , T , frag , unconditionally ,
23032308 rmv = r [ MV ] || MV0 ( ) ,
23042309 modifiedNodesPrev = rmv . mod ,
@@ -2377,15 +2382,24 @@ var
23772382 if ( 'collection' === v . childNodes [ index ] . nodeType )
23782383 v . childNodes . splice . apply ( v . childNodes , [ index , 1 ] . concat ( htmlNode ( view , '' , null , null , [ ] , v . childNodes [ index ] . nodeValue . mapped ( ) ) . childNodes ) ) ;
23792384 }
2380- for ( nodes = { } , index = 0 , count = r . childNodes . length ; index < count ; index ++ )
2385+ vc = v . childNodes . length ;
2386+ keyed = { } ;
2387+ for ( index = 0 ; index < vc ; index ++ )
23812388 {
2382- rnode = r . childNodes [ index ] ;
2383- rnode [ MV ] = rnode [ MV ] || MV0 ( ) ;
2384- // store the keyed nodes in a map
2385- // to be retrieved easily below
2386- if ( rnode [ MV ] . id ) nodes [ '#' + rnode [ MV ] . id ] = rnode ;
2389+ if ( v . childNodes [ index ] . id )
2390+ {
2391+ // there are keyed nodes, associate them in a map for reuse
2392+ for ( index = 0 , count = r . childNodes . length ; index < count ; index ++ )
2393+ {
2394+ rnode = r . childNodes [ index ] ;
2395+ rnode [ MV ] = rnode [ MV ] || MV0 ( ) ;
2396+ // store the keyed nodes in a map
2397+ // to be retrieved and reused easily
2398+ if ( rnode [ MV ] . id ) keyed [ '#' + rnode [ MV ] . id ] = rnode ;
2399+ }
2400+ break ;
2401+ }
23872402 }
2388- vc = v . childNodes . length ;
23892403 count = r . childNodes . length - vc ;
23902404 for ( index = 0 ; index < vc ; index ++ )
23912405 {
@@ -2403,9 +2417,9 @@ var
24032417 {
24042418 morphSingle ( view , r , rnode , vnode , unconditionally ) ;
24052419 }
2406- else if ( vnode . id && ( frag = nodes [ '#' + vnode . id ] ) && eqNodes ( frag , vnode ) )
2420+ else if ( vnode . id && ( frag = keyed [ '#' + vnode . id ] ) && eqNodes ( frag , vnode ) )
24072421 {
2408- r . replaceChild ( frag , rnode ) ;
2422+ r . insertBefore ( frag , rnode ) ;
24092423 morphSingle ( view , r , frag , vnode , unconditionally ) ;
24102424 }
24112425 else if ( 0 === count )
0 commit comments