|
1 | 1 | (function () {
|
2 | 2 |
|
3 |
| - var updateListSection, updateContextSection, updateConditionalSection; |
| 3 | + var updateListSection, updateListObjectSection, updateContextSection, updateConditionalSection; |
4 | 4 |
|
5 | 5 | updateSection = function ( section, value ) {
|
6 | 6 | var fragmentOptions;
|
|
20 | 20 |
|
21 | 21 | // otherwise we need to work out what sort of section we're dealing with
|
22 | 22 |
|
23 |
| - // if value is an array, iterate through |
| 23 | + // if value is an array, or an object with an index reference, iterate through |
24 | 24 | if ( isArray( value ) ) {
|
25 | 25 | updateListSection( section, value, fragmentOptions );
|
26 | 26 | }
|
27 | 27 |
|
28 | 28 |
|
29 | 29 | // if value is a hash...
|
30 | 30 | else if ( isObject( value ) ) {
|
31 |
| - updateContextSection( section, fragmentOptions ); |
| 31 | + if ( section.descriptor.i ) { |
| 32 | + updateListObjectSection( section, value, fragmentOptions ); |
| 33 | + } else { |
| 34 | + updateContextSection( section, fragmentOptions ); |
| 35 | + } |
32 | 36 | }
|
33 | 37 |
|
34 | 38 |
|
|
39 | 43 | };
|
40 | 44 |
|
41 | 45 | updateListSection = function ( section, value, fragmentOptions ) {
|
42 |
| - var i, fragmentsToRemove; |
| 46 | + var i, length, fragmentsToRemove; |
| 47 | + |
| 48 | + length = value.length; |
43 | 49 |
|
44 | 50 | // if the array is shorter than it was previously, remove items
|
45 |
| - if ( value.length < section.length ) { |
46 |
| - fragmentsToRemove = section.fragments.splice( value.length, section.length - value.length ); |
| 51 | + if ( length < section.length ) { |
| 52 | + fragmentsToRemove = section.fragments.splice( length, section.length - length ); |
47 | 53 |
|
48 | 54 | while ( fragmentsToRemove.length ) {
|
49 | 55 | fragmentsToRemove.pop().teardown( true );
|
|
53 | 59 | // otherwise...
|
54 | 60 | else {
|
55 | 61 |
|
56 |
| - if ( value.length > section.length ) { |
| 62 | + if ( length > section.length ) { |
57 | 63 | // add any new ones
|
58 |
| - for ( i=section.length; i<value.length; i+=1 ) { |
| 64 | + for ( i=section.length; i<length; i+=1 ) { |
59 | 65 | // append list item to context stack
|
60 | 66 | fragmentOptions.contextStack = section.contextStack.concat( section.keypath + '.' + i );
|
61 | 67 | fragmentOptions.index = i;
|
|
69 | 75 | }
|
70 | 76 | }
|
71 | 77 |
|
72 |
| - section.length = value.length; |
| 78 | + section.length = length; |
| 79 | + }; |
| 80 | + |
| 81 | + updateListObjectSection = function ( section, value, fragmentOptions ) { |
| 82 | + var id, fragmentsById; |
| 83 | + |
| 84 | + fragmentsById = section.fragmentsById || ( section.fragmentsById = createFromNull() ); |
| 85 | + |
| 86 | + // remove any fragments that should no longer exist |
| 87 | + for ( id in fragmentsById ) { |
| 88 | + if ( value[ id ] === undefined ) { |
| 89 | + fragmentsById[ id ].teardown( true ); |
| 90 | + fragmentsById[ id ] = null; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // add any that haven't been created yet |
| 95 | + for ( id in value ) { |
| 96 | + if ( value[ id ] !== undefined && !fragmentsById[ id ] ) { |
| 97 | + fragmentOptions.contextStack = section.contextStack.concat( section.keypath + '.' + id ); |
| 98 | + fragmentOptions.index = id; |
| 99 | + |
| 100 | + if ( section.descriptor.i ) { |
| 101 | + fragmentOptions.indexRef = section.descriptor.i; |
| 102 | + } |
| 103 | + |
| 104 | + fragmentsById[ id ] = section.createFragment( fragmentOptions ); |
| 105 | + } |
| 106 | + } |
73 | 107 | };
|
74 | 108 |
|
75 | 109 | updateContextSection = function ( section, fragmentOptions ) {
|
|
0 commit comments