attach options to component (#550)

pull/794/head
Rich Harris 8 years ago
parent 87ef5ffefd
commit d8269b3e7f

@ -151,9 +151,9 @@ export default function dom(
// TODO deprecate component.teardown()
builder.addBlock(deindent`
function ${name} ( options ) {
options = options || {};
${options.dev &&
`if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`}
`if ( !options || (!options.target && !options._root) ) throw new Error( "'target' is a required option" );`}
this.options = options;
${generator.usesRefs && `this.refs = {};`}
this._state = ${templateProperties.data
? `@assign( @template.data(), options.data )`

@ -81,6 +81,12 @@ describe("runtime", () => {
code.slice(0, startIndex).split('\n').map(x => spaces(x.length)).join('\n') +
code.slice(startIndex).replace(/export default .+/, "");
acorn.parse(es5, { ecmaVersion: 5 });
if (/Object\.assign/.test(es5)) {
throw new Error(
"cannot use Object.assign in generated code, as it is not supported everywhere"
);
}
} catch (err) {
failed.add(dir);
showOutput(cwd, { shared }); // eslint-disable-line no-console
@ -133,11 +139,6 @@ describe("runtime", () => {
throw err;
}
let usedObjectAssign = false;
Object.assign = () => {
usedObjectAssign = true;
};
global.window = window;
// Put the constructor on window for testing
@ -151,13 +152,13 @@ describe("runtime", () => {
warnings.push(warning);
};
const component = new SvelteComponent({
const options = Object.assign({}, {
target,
hydrate,
data: config.data
});
}, config.options || {});
Object.assign = Object_assign;
const component = new SvelteComponent(options);
console.warn = warn;
@ -183,15 +184,7 @@ describe("runtime", () => {
component.destroy();
assert.equal(target.innerHTML, "");
}
if (usedObjectAssign) {
throw new Error(
"cannot use Object.assign in generated code, as it is not supported everywhere"
);
}
} catch (err) {
Object.assign = Object_assign;
if (config.error && !unintendedError) {
config.error(assert, err);
} else {

@ -0,0 +1,12 @@
export default {
'skip-ssr': true,
html: `<p>from this.options</p>`,
options: {
text: 'from this.options'
},
test(assert, component) {
assert.equal(component.options.text, 'from this.options');
}
};

@ -0,0 +1,11 @@
<p>{{text}}</p>
<script>
export default {
oncreate() {
this.set({
text: this.options.text
});
}
};
</script>
Loading…
Cancel
Save