【问题标题】:How to call a custom JS file in Odoo 8?如何在 Odoo 8 中调用自定义 JS 文件?
【发布时间】:2020-02-04 00:38:16
【问题描述】:

我有一个名为学生的模型。我也有 form viewtree view 用于学生模型。我想要做的是仅在加载 student 模型的表单视图时调用我的自定义 javascript 文件。是否可以?如何做到这一点?谢谢。

我尝试的是.....

openerp.student= function (instance) {

instance.web.FormView.include({

    load_form: function(data) {
        var self = this;
        if (data.model === "student") {
            altert('HELLO');
            console.log('BLAH BLAH');
        }
        return this._super(data);
    },

});
};

【问题讨论】:

  • 您需要将它添加到一个文件夹名称 static 中,在其中另一个文件夹调用它 src odoo 会得到它

标签: javascript odoo odoo-8


【解决方案1】:

您可以覆盖FormViewload_form 方法。

openerp.module_name= function (instance) {

    instance.web.FormView.include({

        load_form: function(data) {
            var self = this;
            if (data.model === "student") {
                // Your custom code
            }
            return this._super(data);
        },

    });
};

要添加上述代码,请检查此链接inherit-or-override-js

【讨论】:

  • 谢谢,虽然我按照你说的方法做了,但还是没用。
  • 除非模型不是student,否则您的代码应该可以工作。
  • 你能出示student模型的声明吗?
  • from openerp.osv import osv, fields class student(osv.osv): _name = 'student' _description = 'Student' _columns = { 'name' : fields.char('Name'), 'age' : fields.integer('Age'), } student()
  • 代码应该可以工作,除非你的javascript文件没有加载
【解决方案2】:

可以通过扩展FormFiew 来添加新的视图模式,就像Odoo 对account_move_line_quickadd 所做的那样。

openerp.your_module_name = function (instance) {
    var _t = instance.web._t,
    _lt = instance.web._lt;
    var QWeb = instance.web.qweb;

    instance.web.your_module_name = instance.web.your_module_name || {};

    instance.web.views.add('student_form', 'instance.web.StudentFormView');

    instance.web.StudentFormView = instance.web.FormView.extend({

        load_form: function(data) {
            var self = this;
            // Add your custom code here
            return this._super(data);
        },
    });
};

您只需将新模式添加到窗口操作即可。

<record id="student_action" model="ir.actions.act_window">
        <field name="name">student.action</field>
        <field name="res_model">student</field>
        <field name="view_mode">student_form,tree</field>
        ...

【讨论】:

  • 谢谢。这行得通。但是,如果我不想要新视图怎么办?我只是想扩展我的表单视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 2016-04-04
  • 2019-10-08
相关资源
最近更新 更多