mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] component: allow using vars with body as props
Consider this scenario: - a variable v (with a body) is defined in a template - it is then passed to a sub component as a prop - and now, it is t-esc-ed. Before this commit, the displayed value was [object object], because the value actually passed to the sub component was a VDomArray (internal structure used to represent nodelists) This issue is actually quite a problem in practice, because values in a templates are translated, but not in attributes. Therefore, using a t-set directive with a body text content is the proper way to have translated values at runtime. We override in this commit the method toString of VDomArray to make sure it is properly displayed. Note that we considered changing the way props were generated (by trying to detect VDomArray, then calling vDomToString), but then the value would not be able to be used in a t-raw. Also, it is quite elegant to be able to format the VDomArray only at the end. closes #670
This commit is contained in:
+22
-14
@@ -90,6 +90,26 @@ function isComponent(obj) {
|
||||
return obj && obj.hasOwnProperty("__owl__");
|
||||
}
|
||||
|
||||
class VDomArray extends Array {
|
||||
toString() {
|
||||
return vDomToString(this);
|
||||
}
|
||||
}
|
||||
|
||||
function vDomToString(vdom: VNode[]): string {
|
||||
return vdom
|
||||
.map((vnode) => {
|
||||
if (vnode.sel) {
|
||||
const node = document.createElement(vnode.sel);
|
||||
const result = patch(node, vnode);
|
||||
return (<HTMLElement>result.elm).outerHTML;
|
||||
} else {
|
||||
return vnode.text;
|
||||
}
|
||||
})
|
||||
.join("");
|
||||
}
|
||||
|
||||
const UTILS: Utils = {
|
||||
zero: Symbol("zero"),
|
||||
toObj(expr) {
|
||||
@@ -111,20 +131,8 @@ const UTILS: Utils = {
|
||||
addNameSpace(vnode) {
|
||||
addNS(vnode.data, vnode.children, vnode.sel);
|
||||
},
|
||||
VDomArray: class VDomArray extends Array {},
|
||||
vDomToString: function (vdom: VNode[]): string {
|
||||
return vdom
|
||||
.map((vnode) => {
|
||||
if (vnode.sel) {
|
||||
const node = document.createElement(vnode.sel);
|
||||
const result = patch(node, vnode);
|
||||
return (<HTMLElement>result.elm).outerHTML;
|
||||
} else {
|
||||
return vnode.text;
|
||||
}
|
||||
})
|
||||
.join("");
|
||||
},
|
||||
VDomArray,
|
||||
vDomToString,
|
||||
getComponent(obj) {
|
||||
while (obj && !isComponent(obj)) {
|
||||
obj = obj.__proto__;
|
||||
|
||||
Reference in New Issue
Block a user