代码修补¶
有时,我们需要定制用户界面的工作方式。许多常见的需求可以通过某些受支持的 API 来满足。例如,所有注册表都是很好的扩展点:字段注册表允许添加/移除专用的字段组件,或者主组件注册表允许添加应始终显示的组件。
然而,在某些情况下这并不足够。在这些情况下,我们可能需要就地修改对象或类。为此,Odoo 提供了实用函数 patch
。它主要用于覆盖/更新某些不受控制的组件或代码的行为。
描述¶
patch
函数位于 @web/core/utils/patch
中:
- patch(objToPatch, extension)¶
- 参数
objToPatch (
object()
) – 需要修补的对象extension (
object()
) – 一个将每个键映射到扩展的对象
- 返回
用于移除修补的函数
patch
函数会就地修改objToPatch
对象(或类),并应用extension
对象中描述的所有键/值对。返回一个取消修补的函数,以便在必要时可以稍后移除修补。大多数修补操作通过使用原生的
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
只能用于修补一次,且不能复制/克隆(请参阅关键字的文档 <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super#description>)。可以使用返回用于修补对象的函数来使其唯一。
const obj1 = {
method() {
doSomething();
},
};
const obj2 = {
method() {
doThings();
},
};
function createExtensionObj() {
return {
method() {
super.method();
doCommonThings();
},
};
}
patch(obj1, createExtensionObj());
patch(obj2, createExtensionObj());
警告
如果一个 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