Odoo10 Javascript Widget动作不起作用:ClientWidget不是一个函数

时间:2017-07-27 12:36:00

标签: javascript widget odoo-10

我还在学习如何正确添加自己的javascript代码。我正在尝试将自己的东西添加到Odoo后端。

我正在仔细阅读“构建界面扩展”指南(https://www.odoo.com/documentation/10.0/howtos/web.html),但它非常不准确或完全过时。

在查看Odoo官方模块(如Account,Project,CRM)后,我编写了类似的内容:

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,
};

});

我的模块xml数据:

<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"/>

这是我的最新代码,当我点击菜单项时没有任何反应,我收到此弹出错误:

TypeError: ClientWidget is not a function

跟踪粘贴在此https://pastebin.com/QLCaLwHC

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

修改

我添加了模板,就像Vishal Khichadiya的例子一样:

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

现在以这种方式添加动作:

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

现在,当我导航到我的菜单项时,我现在收到此错误:

TypeError: this.__getterSetterInternalMap is undefined

此处有新的追溯:https://pastebin.com/phrqXFkz

3 个答案:

答案 0 :(得分:3)

我也是Odoo和JS的新手,我尝试了一些东西,下面给出了:

    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;
   }); 

在/static/src/xml/your_.xml文件中:

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

在/views/your_.xml文件中:

   <?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>

清单 .py:

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

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

它会起作用。请试一试。

答案 1 :(得分:1)

您需要自己设计一个模板。

你会在自己的创建模板中找到参考代码。

将清单文件中的模板文件添加为'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;


});

示例模板代码:

<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>

使用其他xml文件创建assets_backend.xml。

/view/assets_backend.xml - &GT;在这里,您需要添加javasscript文件路径。

 <?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>

答案 2 :(得分:1)

我不熟悉Bounty,我有解决方案link_to_the_screenshot_of_desired_output。它会正常工作。

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); 
   }); 

在/module_name/static/src/xml/your_xml_file.xml中创建xml文件并添加以下代码:

   <?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>

在/module_name/static/src/css/your_css_file.css中创建您的css文件并添加以下代码:

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

在/module_name/views/your_xml_file_.xml中创建视图的xml文件,并添加以下代码:

   <?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>

manifest .py:

中添加以下代码
    'data': [ 
    'views/your_xml_file.xml',
     ],
    'qweb': ['static/src/xml/pet.xml'],