Skip to content

Commit d8269b3

Browse files
committed
attach options to component (#550)
1 parent 87ef5ff commit d8269b3

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

src/generators/dom/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ export default function dom(
151151
// TODO deprecate component.teardown()
152152
builder.addBlock(deindent`
153153
function ${name} ( options ) {
154-
options = options || {};
155154
${options.dev &&
156-
`if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`}
155+
`if ( !options || (!options.target && !options._root) ) throw new Error( "'target' is a required option" );`}
156+
this.options = options;
157157
${generator.usesRefs && `this.refs = {};`}
158158
this._state = ${templateProperties.data
159159
? `@assign( @template.data(), options.data )`

test/runtime/index.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ describe("runtime", () => {
8181
code.slice(0, startIndex).split('\n').map(x => spaces(x.length)).join('\n') +
8282
code.slice(startIndex).replace(/export default .+/, "");
8383
acorn.parse(es5, { ecmaVersion: 5 });
84+
85+
if (/Object\.assign/.test(es5)) {
86+
throw new Error(
87+
"cannot use Object.assign in generated code, as it is not supported everywhere"
88+
);
89+
}
8490
} catch (err) {
8591
failed.add(dir);
8692
showOutput(cwd, { shared }); // eslint-disable-line no-console
@@ -133,11 +139,6 @@ describe("runtime", () => {
133139
throw err;
134140
}
135141

136-
let usedObjectAssign = false;
137-
Object.assign = () => {
138-
usedObjectAssign = true;
139-
};
140-
141142
global.window = window;
142143

143144
// Put the constructor on window for testing
@@ -151,13 +152,13 @@ describe("runtime", () => {
151152
warnings.push(warning);
152153
};
153154

154-
const component = new SvelteComponent({
155+
const options = Object.assign({}, {
155156
target,
156157
hydrate,
157158
data: config.data
158-
});
159+
}, config.options || {});
159160

160-
Object.assign = Object_assign;
161+
const component = new SvelteComponent(options);
161162

162163
console.warn = warn;
163164

@@ -183,15 +184,7 @@ describe("runtime", () => {
183184
component.destroy();
184185
assert.equal(target.innerHTML, "");
185186
}
186-
187-
if (usedObjectAssign) {
188-
throw new Error(
189-
"cannot use Object.assign in generated code, as it is not supported everywhere"
190-
);
191-
}
192187
} catch (err) {
193-
Object.assign = Object_assign;
194-
195188
if (config.error && !unintendedError) {
196189
config.error(assert, err);
197190
} else {
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
'skip-ssr': true,
3+
html: `<p>from this.options</p>`,
4+
5+
options: {
6+
text: 'from this.options'
7+
},
8+
9+
test(assert, component) {
10+
assert.equal(component.options.text, 'from this.options');
11+
}
12+
};
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<p>{{text}}</p>
2+
3+
<script>
4+
export default {
5+
oncreate() {
6+
this.set({
7+
text: this.options.text
8+
});
9+
}
10+
};
11+
</script>

0 commit comments

Comments
 (0)