【Odoo16前端源码分析】env变量

1 定义env变量

/* odoo/addons/web/static/src/env.js */

// -----------------------------------------------------------------------------
// makeEnv
// -----------------------------------------------------------------------------

/**
 * Return a value Odoo Env object
 *
 * @returns {OdooEnv}
 */
export function makeEnv() {
    return {
        bus: new EventBus(),
        services: {},
        debug: odoo.debug,
        _t: () => {
            throw new Error("Translations are not ready yet. Maybe use _lt instead?");
        },

        get isSmall() {
            throw new Error("UI service not initialized!");
        },
    };
}

2 创建env变量,并传给App

/* odoo/addons/web/static/src/start.js */

export async function startWebClient(Webclient) {
    ..

    // setup environment
    const env = makeEnv();
    await startServices(env);

    // start web client
    ..
    const app = new App(Webclient, {
        env,
        templates,
        dev: env.debug,
        translatableAttributes: ["data-tooltip"],
        translateFn: env._t,
    });
    ..
}
/* odoo/addons/web/static/lib/owl/owl.js */

class App extends TemplateSet {
    constructor(Root, config = {}) {
        ..
        const env = config.env || {};
        const descrs = Object.getOwnPropertyDescriptors(env);
        this.env = Object.freeze(Object.create(Object.getPrototypeOf(env), descrs));
        ..
    }
    ..
}

3 成为节点实例的component属性

/* odoo/addons/web/static/lib/owl/owl.js */
class ComponentNode {
    constructor(C, props, app, parent, parentKey) {
        ..
        const env = (parent && parent.childEnv) || app.env;
        this.childEnv = env;
        ..
        this.component = new C(props, env, this);
        ..
    }
..
}

this.component是视图类C(类View)的实例

/* odoo/addons/web/static/src/views/view.js */

export class View extends Component {
    setup() { .. }
    async loadView(props) { .. }
    onWillUpdateProps(nextProps) { .. }
}

但是View类没有构造函数,由父类Component来接管env参数

/* odoo/addons/web/static/lib/owl/owl.js */

class Component {
    constructor(props, env, node) {
        this.props = props;
        this.env = env;
        this.__owl__ = node;
    }
    setup() { }
    render(deep = false) {
        this.__owl__.render(deep === true);
    }
}
Component.template = "";

所以在this.component实例中,也能访问this.env属性

4 WebClient组件实例化的env是app.env,其他组件实例化的env是parent.childEnv

const env = (parent && parent.childEnv) || app.env;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值