1

I have a model called student. I also have form view, tree view for the student model. What I want to do is call my custom javascript file only when the form view of the student model is loaded. Is it possible? How to achieve this? Thanks.

What I tried is .....

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);
    },

});
};
Kenly
  • 24,317
  • 7
  • 44
  • 60
Jeremy Gillbert
  • 133
  • 2
  • 10
  • You need to add it in a folder name static and inside it another folder call it src odoo will get it – imad Feb 04 '20 at 08:57

2 Answers2

2

You can override the load_form method of FormView.

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);
        },

    });
};

To add the above code check this link inherit-or-override-js

Kenly
  • 24,317
  • 7
  • 44
  • 60
1

It is possible to add a new view mode by extending FormFiew as Odoo did with 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);
        },
    });
};

You just need to add the new mode to window action.

<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>
        ...