到目前為止,我們已經介紹了 Odoo 的後端開發,包括 Model、View、Controller 與 API 整合。今天要換個角度,來看看 Odoo 的 前端技術。從 v14 開始,Odoo 引入了 Owl (Odoo Web Library),這是一個現代化的前端框架,讓 Odoo 的 Web 與使用者介面更加模組化與高效。
Owl 是由 Odoo 團隊開發的前端框架,類似 React 或 Vue.js,但針對 Odoo 的需求進行最佳化。其特色包括:
一個簡單的 Owl 元件:
/** hello_component.js **/
import { Component } from "@odoo/owl";
export class HelloComponent extends Component {
static template = "hello_template";
static props = ["name"];
}
對應的模板:
<!-- hello_template.xml -->
<t t-name="hello_template">
<div>
<h3>Hello <t t-esc="props.name"/>!</h3>
</div>
</t>
掛載元件:
import { mount } from "@odoo/owl";
mount(HelloComponent, document.body, { props: { name: "Odoo" } });
顯示結果:
Hello Odoo!
透過 Owl,開發者可以客製化 Odoo 的 Web 客戶端:
Odoo 前端與後端之間透過 RPC (Remote Procedure Call) 溝通。
範例:前端呼叫後端 Model 方法
import { rpc } from "@web/core/network/rpc";
async function getBooks() {
const result = await rpc("/web/dataset/call_kw", {
model: "library.book",
method: "search_read",
args: [],
kwargs: { fields: ["name", "author"] },
});
console.log(result);
}
這段程式會呼叫 library.book
模型,取得書籍清單,並在前端渲染。
優勢
挑戰
Odoo 的前端正在快速演進,Owl 讓開發者能打造更現代化的使用者體驗:
在下一篇文章中,我將介紹 Odoo 的測試框架 (Unit Test 與 Integration Test),幫助開發者提升程式品質與穩定性。