样例代码
odoo.define('uhoo.core.BaseController', function (require) {
"use strict";
const AbstractController = require('web.AbstractController');
const {patch} = require('web.utils');
const core = require('web.core');
const _t = core._t;
/**
* 基础控制器中间层
* 包含 Odoo 原生控制器功能
*/
const BaseController = {
init: function () {
this._super.apply(this, arguments);
// 扩展原生属性
this.customHandlers = {};
this.actionManager = null;
this._title = '';
// 绑定自定义事件处理器
this._bindCustomEvents();
},
// 事件处理
_bindCustomEvents() {
this.on('custom_action', this, this._onCustomAction);
this.on('view_updated', this, this._onViewUpdated);
},
// 继承原生方法
start: async function () {
await this._super.apply(this, arguments);
await this._initializeCustomFeatures();
},
async _initializeCustomFeatures() {
// 初始化自定义功能
},
// 动作处理
_onCustomAction: function (ev) {
const actionName = ev.data.action;
if (this.customHandlers[actionName]) {
this.customHandlers[actionName].call(this, ev);
}
},
// 视图更新处理
_onViewUpdated: function (ev) {
this._updateView(ev.data);
},
_updateView(data) {
// 处理视图更新
},
// 状态管理
_updateState: function (state) {
const result = this._super.apply(this, arguments);
this._processStateUpdate(state);
return result;
},
_processStateUpdate(state) {
// 处理状态更新
},
// 导航处理
_pushState(state) {
this._super.apply(this, arguments);
this._onStateChanged(state);
},
_onStateChanged(state) {
core.bus.trigger('state_changed', state);
},
// 错误处理
_handleError(error) {
console.error('Controller Error:', error);
core.bus.trigger('controller_error', error);
},
// 工具方法
getTitle() {
return this._title;
},
setTitle(title) {
this._title = title;
this.trigger_up('set_title', { title: title });
},
// 清理资源
destroy: function () {
// 清理自定义事件绑定
this.off('custom_action');
this.off('view_updated');
// 清理自定义处理器
this.customHandlers = {};
return this._super.apply(this, arguments);
}
};
patch(AbstractController, 'uhoo.core.BaseController', BaseController);
return BaseController;
});
下面是Odoo的一种写法,核心是patch 的使用.下面对patch 的用法进行说明
1. patch的基本语法:
patch(目标类, 补丁名称, 补丁对象)
2. 一个具体的例子
odoo.define('uhoo.core.BaseController', function (require) {
"use strict";
const AbstractController = require('web.AbstractController');
const {patch} = require('web.utils');
// 定义补丁对象
const BaseController = {
// 扩展init方法
init: function () {
// 调用原始init
this._super.apply(this, arguments);
this.customHandlers = {};
},
// 添加新方法
customMethod: function () {
console.log('This is a new method');
}
};
// 应用补丁
patch(AbstractController, 'uhoo.core.BaseController', BaseController);
return BaseController;
);
代码的效果:
- 直接修改原始类:
// 修改前
const controller = new AbstractController();
controller.customMethod(); // 错误: 方法不存在
// 修改后
const controller = new AbstractController();
controller.customMethod(); // 输出: "This is a new method"
- 与继承的区别:
// 使用继承
class CustomController extends AbstractController {
customMethod() {
console.log('This is a new method');
}
}
// 只有CustomController实例有新方法
const custom = new CustomController();
custom.customMethod(); // 正常工作
// 使用patch
patch(AbstractController, 'name', {
customMethod() {
console.log('This is a new method');
}
});
// 所有AbstractController实例都有新方法
const controller = new AbstractController();
controller.customMethod(); // 正常工作
- 实际应用示例:
// 1. 添加全局功能
const GlobalFeatures = {
init: function () {
this._super.apply(this, arguments);
// 所有控制器都会有这些属性
this.globalState = {};
this.globalHandlers = {};
},
handleGlobalEvent: function (event) {
// 所有控制器都能处理全局事件
}
};
patch(AbstractController, 'global.features', GlobalFeatures);
// 2. 增强现有功能
const EnhancedController = {
_processResult: function (result) {
// 先执行原有处理
result = this._super.apply(this, arguments);
// 添加额外处理
return this._enhanceResult(result);
}
};
patch(AbstractController, 'enhanced.controller', EnhancedController);
// 3. 添加钩子函数
const LifecycleHooks = {
start: async function () {
// 在原始start之前执行
await this._beforeStart();
// 执行原始start
await this._super.apply(this, arguments);
// 在原始start之后执行
await this._afterStart();
}
};
patch(AbstractController, 'lifecycle.hooks', LifecycleHooks);
- patch的特点
// 1. 可以多次patch同一个类
patch(AbstractController, 'patch1', {
method1() { /* ... */ }
});
patch(AbstractController, 'patch2', {
method2() { /* ... */ }
});
// 2. patch是累积的
const controller = new AbstractController();
controller.method1(); // 来自patch1
controller.method2(); // 来自patch2
// 3. 后面的patch可以覆盖前面的
patch(AbstractController, 'patch3', {
method1() { /* 新的实现 */ }
});
- 实际使用场景
// 1. 添加通用功能
const CommonFeatures = {
// 所有控制器都会有这些方法
exportData() { /* ... */ },
importData() { /* ... */ },
printReport() { /* ... */ }
};
patch(AbstractController, 'common.features', CommonFeatures);
// 2. 增强错误处理
const ErrorHandling = {
_handleError(error) {
if (this._isCustomError(error)) {
// 自定义错误处理
return this._handleCustomError(error);
}
return this._super.apply(this, arguments);
}
};
patch(AbstractController, 'error.handling', ErrorHandling);
// 3. 添加状态管理
const StateManagement = {
init() {
this._super.apply(this, arguments);
this.state = new Proxy({}, {
set: (target, key, value) => {
target[key] = value;
this._onStateChange(key, value);
return true;
}
});
}
};
patch(AbstractController, 'state.management', StateManagement);
主要优势:
全局影响
- 修改会影响所有实例
- 不需要修改原始代码
- 便于全局功能扩展
灵活性
- 可以随时添加或移除功能
- 支持多个patch叠加
- 可以根据条件应用patch
维护性
- 补丁代码集中管理
- 便于追踪修改
- 易于更新和回滚
性能
- 避免继承链过长
- 减少内存使用
- 提高代码执行效率

发表评论