修补代码¶
有时,我们需要自定义 UI 的工作方式。许多常见需求已由一些受支持的 API 覆盖。例如,所有注册表都是良好的扩展点:字段注册表允许添加/移除专用字段组件,而主组件注册表允许添加应始终显示的组件。
然而,有些情况仅靠这些还不够。在这些情况下,我们可能需要就地修改某个对象或类。为此,Odoo 提供了实用函数 patch。它主要用于覆盖/更新自己无法控制的其他组件或代码的行为。
说明¶
patch 函数位于 @web/core/utils/patch 中:
- patch(objToPatch, extension)¶
- 参数
objToPatch (
object()) – 应被修补的对象extension (
object()) – 将每个键映射到一个扩展的对象
- 退货
用于移除补丁的函数
patch函数会就地修改objToPatch对象(或类),并应用extension对象中描述的所有键/值。它会返回一个 unpatch 函数,以便日后在需要时移除该补丁。大多数修补操作都可通过原生
super关键字访问父级值(请参见下面的示例)。
修补简单对象¶
下面是一个如何修补对象的简单示例:
import { patch } from "@web/core/utils/patch";
const object = {
field: "a field",
fn() {
// do something
},
};
patch(object, {
fn() {
// do things
},
});
在修补函数时,我们通常希望能够访问 parent 函数。为此,只需使用原生的 super 关键字:
patch(object, {
fn() {
super.fn(...arguments);
// do other things
},
});
警告
super 只能用于方法中,不能用于函数中。这意味着以下写法在 JavaScript 中是无效的。
const obj = {
a: function () {
// Throws: "Uncaught SyntaxError: 'super' keyword unexpected here"
super.a();
},
b: () => {
// Throws: "Uncaught SyntaxError: 'super' keyword unexpected here"
super.b();
},
};
getter 和 setter 同样受支持:
patch(object, {
get number() {
return super.number / 2;
},
set number(value) {
super.number = value;
},
});
修补 JavaScript 类¶
patch 函数设计为可用于任何对象或 ES6 类。
然而,由于 JavaScript 类使用原型继承,当希望修补类中的标准方法时,我们实际上需要修补的是 prototype:
class MyClass {
static myStaticFn() {...}
myPrototypeFn() {...}
}
// this will patch static properties!!!
patch(MyClass, {
myStaticFn() {...},
});
// this is probably the usual case: patching a class method
patch(MyClass.prototype, {
myPrototypeFn() {...},
});
此外,JavaScript 以特殊的原生方式处理构造函数,使其无法被修补。唯一的变通方法是在原始构造函数中调用一个方法,然后改为修补该方法:
class MyClass {
constructor() {
this.setup();
}
setup() {
this.number = 1;
}
}
patch(MyClass.prototype, {
setup() {
super.setup(...arguments);
this.doubleNumber = this.number * 2;
},
});
警告
无法直接修补类的 constructor!
修补组件¶
组件由 JavaScript 类定义,因此上述信息依然适用。出于这些原因,Owl 组件应使用 setup 方法,这样它们也能很容易地被修补(参见 最佳实践 一节)。
patch(MyComponent.prototype, {
setup() {
useMyHook();
},
});
移除补丁¶
patch 函数会返回其对应物。这主要用于测试场景:在测试开始时修补某些内容,并在结束时取消修补。
const unpatch = patch(object, { ... });
// test stuff here
unpatch();
将同一补丁应用于多个对象¶
有时可能需要将同一个补丁应用于多个对象,但由于 super 关键字的工作方式,extension 只能用于修补一次,且无法复制/克隆(查看该关键字的文档)。可以使用一个返回用于修补的对象的函数来使其保持唯一。
const obj1 = {
method() {
doSomething();
},
};
const obj2 = {
method() {
doThings();
},
};
function createExtensionObj() {
return {
method() {
super.method();
doCommonThings();
},
};
}
patch(obj1, createExtensionObj());
patch(obj2, createExtensionObj());
警告
如果某个 extension 基于另一个 extension,则这两个扩展应分别应用。不要复制/克隆扩展。
const object = {
method1() {
doSomething();
},
method2() {
doAnotherThing();
},
};
const ext1 = {
method1() {
super.method1();
doThings();
},
};
const invalid_ext2 = {
...ext1, // this will not work: super will not refer to the correct object in methods coming from ext1
method2() {
super.method2();
doOtherThings();
},
};
patch(object, invalid_ext2);
object.method1(); // throws: Uncaught TypeError: (intermediate value).method1 is not a function
const valid_ext2 = {
method2() {
super.method2();
doOtherThings();
},
};
patch(object, ext1); // first patch base extension
patch(object, valid_ext2); // then the new one
object.method1(); // works as expected