2

I have created a website template in ODOO V8:

<template id="assets_frontend" inherit_id="website.assets_frontend" name="MyTemplate">
    <xpath expr="." position="inside">
        <script type="text/javascript" src="/my_module/static/src/js/main.js"></script>
    </xpath>
</template>


<template id="my_template">
    <script type="text/javascript" src="/my_module/static/src/js/main.js"/>
    <div>
        <table>
            <tr>
                <td>Email: <input type="text" id="email"/></td>
            </tr>
            <tr>
                <td colspan="3">
                    <input type="button" value="Submit" onclick="submitEmail();"/>
                </td>
            </tr>
        </table>
    </div>
</template>

And the contents in main.js file are:

$(document).ready(function () {
    "use strict";

    function submitEmail() {
        var self = this;
        var website = openerp.website;
        var Users = new openerp.website.Model('res.users');
        // ...
    }
})();

But on clicking the submit button in the template it shows the following error in the browser console.

Uncaught ReferenceError: openerp is not defined

I need to access the ODOO models and methods in it from the main.js file. How can I solve the above error or is there any way I can access the models and methods defined in ODOO classes from a javascript file?

This error is not in the base modules(like website_sale) in ODOO and the error is only in new modules I have created.

Marc Dix
  • 1,864
  • 15
  • 29
Gopakumar N G
  • 1,775
  • 1
  • 23
  • 40

2 Answers2

1

inside $(document).ready you can't access openerp .

In the js file create a method same name as your module(not model) and pass two parameter instance and module

Now in the method you can access the module using instance of openerp like:

function my_module(instance, module){
module = instance.point_of_sale;
var QWeb = instance.web.qweb;
_t = instance.web._t;
var OrderSuper = module.ProductListWidget;
}
Prakash Kumar
  • 2,554
  • 2
  • 18
  • 28
0

You need read this document https://www.odoo.com/documentation/8.0/reference/javascript.html and take attention on "Subclassing Widget". This is an example of a module in Odoo. You have to program something like that example.

Fernando
  • 99
  • 2
  • 9