Skip to content

Commit dc4e929

Browse files
committedFeb 27, 2017
Merge branch 'master' of https://github.com/sveltejs/svelte
2 parents 633c682 + 0d29d46 commit dc4e929

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed
 

‎src/generators/dom/visitors/Element.js

+4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ export default {
111111
return Component.leave( generator, node );
112112
}
113113

114+
if ( node.initialUpdate ) {
115+
generator.current.builders.init.addBlock( node.initialUpdate );
116+
}
117+
114118
generator.pop();
115119
}
116120
};

‎src/generators/dom/visitors/attributes/binding/index.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,27 @@ export default function createBinding ( generator, node, attribute, current, loc
116116
}
117117
` );
118118
} else {
119-
const updateElement = `${local.name}.${attribute.name} = ${contextual ? attribute.value : `root.${attribute.value}`}`;
119+
let updateElement;
120+
121+
if ( node.name === 'select' ) {
122+
// TODO select multiple
123+
const value = generator.current.getUniqueName( 'value' );
124+
const i = generator.current.getUniqueName( 'i' );
125+
const option = generator.current.getUniqueName( 'option' );
126+
127+
updateElement = deindent`
128+
var ${value} = ${contextual ? attribute.value : `root.${attribute.value}`};
129+
for ( var ${i} = 0; ${i} < ${local.name}.options.length; ${i} += 1 ) {
130+
var ${option} = ${local.name}.options[${i}];
131+
if ( ${option}.__value === ${value} ) {
132+
${option}.selected = true;
133+
break;
134+
}
135+
}
136+
`;
137+
} else {
138+
updateElement = `${local.name}.${attribute.name} = ${contextual ? attribute.value : `root.${attribute.value}`};`;
139+
}
120140

121141
generator.uses.addEventListener = true;
122142
generator.uses.removeEventListener = true;
@@ -130,20 +150,16 @@ export default function createBinding ( generator, node, attribute, current, loc
130150
}
131151
132152
addEventListener( ${local.name}, '${eventName}', ${handler} );
133-
${updateElement};
134153
` );
135154

155+
node.initialUpdate = updateElement;
156+
136157
local.update.addLine(
137-
`if ( !${local.name}_updating ) ${updateElement};`
158+
`if ( !${local.name}_updating ) ${updateElement}`
138159
);
139160

140161
generator.current.builders.teardown.addLine( deindent`
141162
removeEventListener( ${local.name}, '${eventName}', ${handler} );
142163
` );
143164
}
144-
145-
if ( node.name === 'select' ) {
146-
generator.hasComplexBindings = true;
147-
local.init.addLine( `component._bindings.push( ${handler} )` );
148-
}
149165
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default {
2+
skip: true, // selectedOptions doesn't work in JSDOM???
3+
4+
html: `
5+
<p>selected: b</p>
6+
7+
<select>
8+
<option>a</option>
9+
<option>b</option>
10+
<option>c</option>
11+
</select>
12+
13+
<p>selected: b</p>
14+
`,
15+
16+
data: {
17+
selected: 'b'
18+
},
19+
20+
test ( assert, component, target ) {
21+
const select = target.querySelector( 'select' );
22+
const options = [ ...target.querySelectorAll( 'option' ) ];
23+
24+
assert.equal( select.value, 'b' );
25+
assert.ok( options[1].selected );
26+
27+
component.teardown();
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>selected: {{selected}}</p>
2+
3+
<select bind:value='selected'>
4+
<option value="a">a</option>
5+
<option value="b">b</option>
6+
<option value="c">c</option>
7+
</select>
8+
9+
<p>selected: {{selected}}</p>

0 commit comments

Comments
 (0)