mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] component: capture ref key in closure
This is what commit d3197e9865 should have
done. Many changes should be done on normal nodes and on components!
closes #77
This commit is contained in:
+12
-11
@@ -120,9 +120,9 @@ const UTILS = {
|
|||||||
elm.classList.add(name + "-leave");
|
elm.classList.add(name + "-leave");
|
||||||
elm.classList.add(name + "-leave-active");
|
elm.classList.add(name + "-leave-active");
|
||||||
const finalize = () => {
|
const finalize = () => {
|
||||||
elm.classList.remove(name + "-leave-active");
|
elm.classList.remove(name + "-leave-active");
|
||||||
elm.classList.remove(name + "-enter-to");
|
elm.classList.remove(name + "-enter-to");
|
||||||
rm();
|
rm();
|
||||||
};
|
};
|
||||||
this.nextFrame(() => {
|
this.nextFrame(() => {
|
||||||
elm.classList.remove(name + "-leave");
|
elm.classList.remove(name + "-leave");
|
||||||
@@ -1193,22 +1193,23 @@ const widgetDirective: Directive = {
|
|||||||
for (let [event, method] of events) {
|
for (let [event, method] of events) {
|
||||||
ctx.addLine(`w${widgetID}.on('${event}', owner, owner['${method}'])`);
|
ctx.addLine(`w${widgetID}.on('${event}', owner, owner['${method}'])`);
|
||||||
}
|
}
|
||||||
let ref = node.getAttribute("t-ref");
|
|
||||||
let refExpr = ref
|
|
||||||
? `context.refs[${ctx.formatExpression(ref)}] = w${widgetID};`
|
|
||||||
: "";
|
|
||||||
|
|
||||||
ctx.addLine(`def${defID} = w${widgetID}._prepare();`);
|
ctx.addLine(`def${defID} = w${widgetID}._prepare();`);
|
||||||
ctx.closeIf();
|
ctx.closeIf();
|
||||||
ctx.closeIf();
|
ctx.closeIf();
|
||||||
|
let ref = node.getAttribute("t-ref");
|
||||||
|
let refExpr = "";
|
||||||
|
let refKey: string = "";
|
||||||
|
if (ref) {
|
||||||
|
refKey = `ref${ctx.generateID()}`;
|
||||||
|
ctx.addLine(`const ${refKey} = ${ctx.formatExpression(ref)}`);
|
||||||
|
refExpr = `context.refs[${refKey}] = w${widgetID};`;
|
||||||
|
}
|
||||||
|
|
||||||
let finalizeWidgetCode = `w${widgetID}.${
|
let finalizeWidgetCode = `w${widgetID}.${
|
||||||
keepAlive ? "unmount" : "destroy"
|
keepAlive ? "unmount" : "destroy"
|
||||||
}()`;
|
}()`;
|
||||||
if (ref) {
|
if (ref) {
|
||||||
finalizeWidgetCode += `;delete context.refs[${ctx.formatExpression(
|
finalizeWidgetCode += `;delete context.refs[${refKey}]`;
|
||||||
ref
|
|
||||||
)}]`;
|
|
||||||
}
|
}
|
||||||
ctx.addIf(`isNew${widgetID}`);
|
ctx.addIf(`isNew${widgetID}`);
|
||||||
ctx.addLine(
|
ctx.addLine(
|
||||||
|
|||||||
@@ -901,6 +901,21 @@ describe("composition", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("refs in a loop", async () => {
|
||||||
|
class ParentWidget extends Widget {
|
||||||
|
inlineTemplate = `<div>
|
||||||
|
<t t-foreach="state.items" t-as="item">
|
||||||
|
<t t-widget="Child" t-ref="item" t-key="item"/>
|
||||||
|
</t>
|
||||||
|
</div>`;
|
||||||
|
state = { items: [1, 2, 3] };
|
||||||
|
widgets = { Child: Widget };
|
||||||
|
}
|
||||||
|
const parent = new ParentWidget(env);
|
||||||
|
await parent.mount(fixture);
|
||||||
|
expect(Object.keys(parent.refs)).toEqual(["1", "2", "3"]);
|
||||||
|
});
|
||||||
|
|
||||||
test("parent's elm for a children === children's elm, even after rerender", async () => {
|
test("parent's elm for a children === children's elm, even after rerender", async () => {
|
||||||
const widget = new WidgetA(env);
|
const widget = new WidgetA(env);
|
||||||
await widget.mount(fixture);
|
await widget.mount(fixture);
|
||||||
|
|||||||
Reference in New Issue
Block a user