Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to add a Javascript function to object through inheritance

By
Eric
on 6/24/15, 3:47 PM 1,744 views

How can I create a custom javascript file that adds a new property and function to an inherited object, so that my custom menu item will open a new website?

So far, I created a custom web module that inherits from the original web module in Openerp 7.  

I added a new menu item in the User Menu by extending from the UserMenu template in the base.xml file.

Here is my custom xml code that adds a new menu item to the User Menu template.

(custom xml file.  This new menu item does appear in the User Menu )

<templates>

    <t t-extend="UserMenu">

        <t t-jquery="ul" t-operation="append">

           <li><a href="#" data-menu="website">Website Menu Item</a></li>

        </t>

    </t>

</templates>


base.xml (original)

<t t-name="UserMenu">

    <span class="oe_user_menu oe_topbar_item oe_dropdown_toggle oe_dropdown_arrow">

        <img class="oe_topbar_avatar" t-att-data-default-src="_s + '/web/static/src/img/user_menu_avatar.png'"/>

        <span class="oe_topbar_name"/>

        <ul class="oe_dropdown_menu">

            <li><a href="#" data-menu="settings">Preferences</a></li>

            <li><a href="#" data-menu="account">My OpenERP.com account</a></li>

            <li><a href="#" data-menu="about">About OpenERP</a></li>

            <li><a href="#" data-menu="help">Help</a></li>

            <li><a href="#" data-menu="logout">Log out</a></li>

        </ul>

    </span>

</t>


What I want to do now is inherit the UserMenu object that is defined in the web module's chrome.js file, into my custom javascript file.   Then I want to add a property to the UserMenu object so that a new website is opened when the menu item is clicked

Here is what I have so far:

 

openerp.web_custom = function(instance){

     // Add the on_menu_website property to the UserMenu object

     instance.web.UserMenu.on_menu_website = function() {

         //open the website  ...  its not working though

         window.open('http://www.website.com', '_blank');

     }

}


Here is the original js file code:

instance.web.UserMenu = instance.web.Widget.extend({

    template: "UserMenu",

    init: function(parent) {

       ... some code here ...

    },

    start: function() {

        ... some code here ...

    },

    do_update: function () {

        ... some code here ...

    },

    on_menu_help: function() {

        ... some code here ...

    },

    on_menu_logout: function() {

        ... some code here ...

    },

    on_menu_settings: function() {

        ... some code here ...

    },

    on_menu_account: function() {

        ... some code here ...

    },

    on_menu_about: function() {

        ... some code here ...

    },

});


One thing to note, if I add my new code the the original web module files (base.xml, chrome.js) then it works fine.  But I want to be able to create a custom web module so that I can add new functionality without modifying the original web module.  So my question is, how can I create a custom javascript file that adds a new property and function so that my menu item will open the new website?  Thank you for your help

1
Eric
On 6/24/15, 5:33 PM

I changed my custom javascript file to this. Then it worked


openerp.web_custom = function(instance){

     instance.web.UserMenu.include({

          on_menu_website: function () {

               window.open('http://www.website.com', '_blank');

          },

     });

}

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/24/15, 3:47 PM
Seen: 1744 times
Last updated: 6/24/15, 5:33 PM