Skip to content

Commit 1c979eb

Browse files
committed
list sections from objects, as per #115. experimental!
1 parent e820e78 commit 1c979eb

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

src/internal/fragments/DomFragment/DomSection.js

+9
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,18 @@ DomSection.prototype = {
168168
},
169169

170170
teardownFragments: function ( detach ) {
171+
var id;
172+
171173
while ( this.fragments.length ) {
172174
this.fragments.shift().teardown( detach );
173175
}
176+
177+
if ( this.fragmentsById ) {
178+
for ( id in this.fragmentsById ) {
179+
this.fragmentsById[ id ].teardown();
180+
this.fragmentsById[ id ] = null;
181+
}
182+
}
174183
},
175184

176185
render: function ( value ) {

src/internal/fragments/utils/updateSection.js

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function () {
22

3-
var updateListSection, updateContextSection, updateConditionalSection;
3+
var updateListSection, updateListObjectSection, updateContextSection, updateConditionalSection;
44

55
updateSection = function ( section, value ) {
66
var fragmentOptions;
@@ -20,15 +20,19 @@
2020

2121
// otherwise we need to work out what sort of section we're dealing with
2222

23-
// if value is an array, iterate through
23+
// if value is an array, or an object with an index reference, iterate through
2424
if ( isArray( value ) ) {
2525
updateListSection( section, value, fragmentOptions );
2626
}
2727

2828

2929
// if value is a hash...
3030
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+
}
3236
}
3337

3438

@@ -39,11 +43,13 @@
3943
};
4044

4145
updateListSection = function ( section, value, fragmentOptions ) {
42-
var i, fragmentsToRemove;
46+
var i, length, fragmentsToRemove;
47+
48+
length = value.length;
4349

4450
// 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 );
4753

4854
while ( fragmentsToRemove.length ) {
4955
fragmentsToRemove.pop().teardown( true );
@@ -53,9 +59,9 @@
5359
// otherwise...
5460
else {
5561

56-
if ( value.length > section.length ) {
62+
if ( length > section.length ) {
5763
// add any new ones
58-
for ( i=section.length; i<value.length; i+=1 ) {
64+
for ( i=section.length; i<length; i+=1 ) {
5965
// append list item to context stack
6066
fragmentOptions.contextStack = section.contextStack.concat( section.keypath + '.' + i );
6167
fragmentOptions.index = i;
@@ -69,7 +75,35 @@
6975
}
7076
}
7177

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+
}
73107
};
74108

75109
updateContextSection = function ( section, fragmentOptions ) {

0 commit comments

Comments
 (0)