Odoo10 Javascript Widget action not working: ClientWidget is not a function

回眸只為那壹抹淺笑 提交于 2019-12-24 09:37:08

问题


I'm still learning how to properly add your own javascript code. I'm trying to add my own stuff into Odoo backend.

I was carefully reading "Building Interface Extensions" guide (https://www.odoo.com/documentation/10.0/howtos/web.html), but it is ether badly inaccurate or completely outdated.

After looking at Odoo official modules like Account, Project, CRM I've coded something like this:

odoo.define('rhp.main', function (require) {
"use strict";

var core = require('web.core');
var _t = core._t;
var _lt = core._lt;
var Widget = require('web.Widget');
var QWeb = core.qweb;

var Explorer = Widget.extend({
    init: function(parent, options) {
        console.log('Explorer inited');
    },
    start: function() {
        console.log('Explorer started');
    },
});

/* NONE OF THESE WORK OK */
//core.view_registry.add('exploreraction', Explorer);
//core.action_registry.add('exploreraction', function() { return new Explorer(); });
core.action_registry.add('exploreraction', 'rhp.main.Explorer');
//odoo.client_actions.add('exploreraction', Explorer);

/* "Main loaded" is successfully printed in console */
console.log("Main loaded");

return {
    Explorer: Explorer,
};

});

My module xml data:

<record id="explorer_action" model="ir.actions.client">
    <field name="name">Document Explorer</field>
    <field name="tag">exploreraction</field>
    <field name="target">main</field>
</record>

<menuitem name="Documents" id="main_docs" />
<menuitem name="Document Explorer" id="rhp_explorer" parent="rhp.main_docs" action="rhp.explorer_action"/>

This is my latest code and when I click the menu item nothing happens and I get this popup error:

TypeError: ClientWidget is not a function

Traceback pasted here https://pastebin.com/QLCaLwHC

=========================================

EDIT:

I added the template, like in Vishal Khichadiya's example:

<t t-name="exploreraction" >
    <div id="exploreraction_div">
        test
    </div>
</t>

Action is now added this way:

core.action_registry.add('exploreraction', Explorer);

Now when I navigate to my menu item I'm now getting this error:

TypeError: this.__getterSetterInternalMap is undefined

New traceback here: https://pastebin.com/phrqXFkz


回答1:


I'm also new to Odoo and JS, and I have tried something and that is given below:

    In your /static/src/js/your_.js file, add the code:

    odoo.define('zbtoken.map', function (require) {
        "use strict";

    var Widget = require('web.Widget');
    var core = require('web.core');
    var utils = require('web.utils');


    var HomePage = Widget.extend({
       template: "HelloJS",

       init: function(parent) {
            this._super(parent);
            console.log("Hello JS, I'm inside of init.");
       },


    start: function() {
            console.log("Hello JS, I'm inside of start.");
     },
   });

   core.action_registry.add('HelloJS', HomePage);

   return HomePage;
   }); 

In your /static/src/xml/your_.xml file:

    <?xml version="1.0"  encoding="UTF-8"?>
    <templates xml:space="preserve">
       <t t-name="HelloJS">
          <div> </div>
       </t>
    </templates>

In your /views/your_.xml file:

   <?xml version="1.0" encoding="UTF-8"?>
    <odoo>
      <data>

    <template id="assets_backend" name="petstore" 
        inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript"         
            src="/your_module_name/static/src/js/your_js_file.js">
            </script>
        </xpath>
    </template> 

    <record id="hellojs_id" model="ir.actions.client">
        <field name="name">HelloJS</field>
        <field name="tag">HelloJS</field>
    </record>
     <menuitem id="hellojs_menu" 
            name="HelloJS" 
            action="hellojs_id"/>
     </data>
     </odoo>

In manifest.py:

     'data': [ 
     'views/your_.xml file',  
      ],

     'qweb': ['static/src/xml/your_.xml file'],

It will work. Please try it.




回答2:


You need to design a Template by your self.

You'll find reference code in odoo it self for create template.

add you template file in manifest file as 'qweb': ["static/src/xml/my_template.xml"]

odoo.define('rhp.main', function (require) {
"use strict";

var core = require('web.core');
var _t = core._t;
var _lt = core._lt;
var Widget = require('web.Widget');
var QWeb = core.qweb;

var Explorer = Widget.extend({
    init: function(parent, options) {
        console.log('Explorer inited');
    },
    start: function() {
        console.log('Explorer started');
    },
});
//exploreraction is must be your template name        
core.action_registry.add('exploreraction', Explorer);

return Explorer;


});

Examplea Template code:

<t t-name="exploreraction" >
    <div id="my_temp_id">
    <div class="ex_button">
        <div class="cancle_btn">
            <button class="btn btn-danger">Cancle</button>
        </div>
        <div class="Add_btn">
            <button class="btn btn-success">Add </button>
        </div>
    </div>
    </div>
</t>

create assets_backend.xml with your other xml files.

/view/assets_backend.xml --> Here you need to add javasscript file path.

 <?xml version="1.0"?>
 <odoo>
   <data>
    <template id="assets_backend" inherit_id="web.assets_backend">
        <xpath expr="script[last()]" position="after">

            <script type="text/javascript" src="/static/src/js/rhp_main.js"></script>
        </xpath>
    </template>
    <template id="assets_common" inherit_id="web.assets_common">
        <xpath expr="script[last()]" position="after">

        </xpath>
    </template>
  </data>
 </odoo>



回答3:


I am not familiar with Bounty and I have solution for this link_to_the_screenshot_of_desired_output. It will work fine.

Create your js file in /module_name/static/src/js/your_js_file.js and 
add the following code :


   odoo.define('mypetstore.test_js', function (require) {
    "use strict";

     var Widget = require('web.Widget');
     var core = require('web.core');
     var Model = require('web.Model');

     var ProductsWidget = Widget.extend({
         template: "ProductsWidget",
         init: function(parent, products, color) {
         this._super(parent);
         this.products = products;
         this.color = color;
    },
  });

  var HomePage = Widget.extend({

    start: function() {
        var products = new ProductsWidget(
            this, ["cpu", "mouse", "keyboard", "graphic card", 
                  "screen"], "#00FF00");
        products.appendTo(this.$el);
      },    
    });
    core.action_registry.add('ProductsWidget', HomePage); 
   }); 

Create your xml file in /module_name/static/src/xml/your_xml_file.xml and add the following code:

   <?xml version="1.0"  encoding="UTF-8"?>
     <templates xml:space="preserve">
       <t t-name="ProductsWidget">
         <div>
            <t t-foreach="widget.products" t-as="product">
               <span class="oe_products_item"
                  t-attf-style="background-color: {{ widget.color 
                 }};">
                <t t-esc="product"/>
                </span>
              <br/>
            </t>
         </div>
        </t>
     </templates>

Create your css file in /module_name/static/src/css/your_css_file.css and add the following code:

     .oe_products_item {
     display: inline-block;
     padding: 3px;
     margin: 5px;
     border: 1px solid black;
     border-radius: 3px;
     }

Create your view's xml file in /module_name/views/your_xml_file_.xml and add the following code :

   <?xml version="1.0" encoding="UTF-8"?>
    <odoo>
      <data>
        <template id="assets_backend" name="give_name" 
           inherit_id="web.assets_backend">
          <xpath expr="." position="inside">
           <script type="text/javascript" 
             src="/module_name/static/src/js/your_js_file_name.js">
           </script>
           <link href="/module_name/static/src/css/your_css_file.css" 
             rel="stylesheet"></link>
           </xpath>
         </template>
         <record id="template_id" model="ir.actions.client">
            <field name="name">ProductsWidget</field>
            <field name="tag">ProductsWidget</field>
        </record>
        <menuitem id="home_page_menu" 
            name="Home Page"
            action="template_id"/>  
       </data>
      </odoo>

Add the following codes in manifest.py:

    'data': [ 
    'views/your_xml_file.xml',
     ],
    'qweb': ['static/src/xml/pet.xml'],


来源:https://stackoverflow.com/questions/45351011/odoo10-javascript-widget-action-not-working-clientwidget-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!