Juan Salcedo
Juan Salcedo

Reputation: 1668

JavaScript is not fired! ODOO

I've created a basic module for testing my .js function, but I don't know why it isn't firing? This is the architecture of my module:

a_js
 |static
     |src
        |js
          a_js.js
 |view
   |a_js_view.xml
   |call.xml
 |__init__.py
 |__openerp__.py
 |a_js.py

This are my files:

a_js/__init__.py

import a_js

Here is my a_js/__openerp__.py

#a_js/__openerp__.py
# -*- coding: utf-8 -*-
{
'name': 'Pruebas JS',
    'version': '0.1',
    'website' : '',
    'category': '',
    'summary': '',
    'description': """ - """,
    'author': 'JS',
    'depends': [
    'base',
    'web',
    ],
    'data': [
    "view/a_js_view.xml",
    "view/call.xml",
    ],
        'demo': [
    ],
    'test': [
    ],
    'installable': True,
    'auto_install': False,
}

My a_js/a_js.py

# -*- coding: utf-8 -*-
from openerp import models,fields,api
class a_js(models.Model):    
    _name="a.js"
    numeros = fields.Integer('Números')
    letras = fields.Char('Letras')
    tiempo = fields.Date('Fecha')

My a_js/view/a_js_view.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <menuitem name="JS" id="js_root_menu"/>
        <menuitem name="JS" parent="js_root_menu" id="js_menu"/>
        <record id="a_js_form" model="ir.ui.view">
            <field name="name">a.js.form</field>
            <field name="model">a.js</field>
            <field name="arch" type="xml">
                <form string="Invoice">
                    <header>
                    </header>
                    <sheet string="Datos">
                        <label string="Datos de Prueba" />
                        <field name="numeros" class="id_num"/>
                        <field name="letras"/>
                        <field name="tiempo"/>
                    </sheet>
                </form>
            </field>
         </record>

        <record id="a_js_action" model="ir.actions.act_window">         
            <field name="res_model">a.js</field>
            <field name="view_type">form</field>
            <field name="name">JS</field>
            <field name="view_mode">tree,form</field>
        </record>
        <menuitem name="JS" parent="js_menu"  action="a_js_action" id="a_js_menu"/>
    </data>
</openerp>

Here is my a_js/view/call.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <template id="assets_backend" name="a_js assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/a_js/static/src/js/a_js.js"></script>
            </xpath>
        </template>
    </data>
</openerp>

And here is my a_js/static/src/js/a_js.js file:

openerp.a_js = function(instance) {
var QWeb = openerp.web.qweb;
    _t = instance.web._t;
instance.web.FormView.include({
    on_loaded: function(data) {
        var self = this;
        $('.id_num').keypress(function isNumber(evt) {
            evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                return false;
            }
            return true;
        });
        return self._super(data);
    },
});
};

I want to only allow numbers entry in my numeros field, Why my js function is not fired? Please share with as some examples about how to do that, or a guideline please!

Upvotes: 3

Views: 2001

Answers (1)

Your script will be added in top of the assets,

<xpath expr="." position="inside"> 

So when you are doing this make sure view_form.js is already loaded before your script.

I think you need to do

<xpath expr="//script[@src='/web/static/src/js/view_tree.js']" position="after">
     <script type="text/javascript" src="your script file path"></script>
</xpath>

Another thing first check by putting alert function whether your script will be loaded or not, if it's loaded then put alert inside function whether it's calling or not.

Upvotes: 1

Related Questions