Anwar
Anwar

Reputation: 4246

include a javascript code only for a certain view type

I want to add a javascript code only when the user create a record or update a record for the res.partner model.

I already tryied to include a javascript file using this technique (and I will explain what it does below) :

<odoo>
    <data>
        <template id="my_module_script" name="my module script" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/my_module/static/src/js/script.js"></script>
            </xpath>
        </template>
    </data>
</odoo>

This works great, my javascript file is now included... but in any views (and not only for my specific partner create/update view).

QUESTION

Is there any way I can tell odoo to load a specific javascript (or css file as well) for a specific view type (I would like for this example the javascript file loaded for base.view_partner_form view) ?

Upvotes: 1

Views: 2099

Answers (2)

Vinh VO
Vinh VO

Reputation: 703

If you include your javascript file, it will be compiled to web.assets_backend.js every time the module is installed.

And web.assets_backend.js is loaded on every time the backend page is loaded, not when a view is loaded.

Odoo is designed to be a SPA. The javascript files are loaded on the first page load.

So I don't think this should be done by this way (add custom js to a view), but by the Odoo's way: Trigger your code by specific actions.

Upvotes: 1

Keval Mehta
Keval Mehta

Reputation: 664

Ohh... That's sounds poor. Not to worry we will try to solve it. :)

1) You just need to create a constructor in js of your model. Like this way:

new instance.web.Model("res.partner").call()

Here in call method you can call any python method or else call your view also.

2) Also you can do this by passing url to your js file. Kind of action url. Like this

action_url = _.str.sprintf('/web?db=%s#id=%s&view_type=form&model=res.parnter', db, meeting_id);

3) Another way is to pass action. Whrn url is redirected to that then this action is getting called from js.

on_follower_clicked: function  (event) {
        event.preventDefault();
        var partner_id = $(event.target).data('partner');
        var state = {
            'model': 'res.partner',
            'id': partner_id,
            'title': this.record_name
        };
        session.webclient.action_manager.do_push_state(state);
        var action = {
            type:'ir.actions.act_window',
            view_type: 'form',
            view_mode: 'form',
            res_model: 'res.partner',
            views: [[false, 'form']],
            res_id: partner_id,
        }
        this.do_action(action);
    },

4) You can also get current model by this.model This will return current model also new instance.web.Model(self.dataset.model) get current dataset model.

You can refere base code for this too. There are lots of examples are for all of this methods.

Upvotes: 1

Related Questions