簡體   English   中英

Odoo10 Javascript Widget操作不起作用:ClientWidget不是函數

[英]Odoo10 Javascript Widget action not working: ClientWidget is not a function

我仍在學習如何正確添加自己的JavaScript代碼。 我正在嘗試將自己的內容添加到Odoo后端。

我正在仔細閱讀“構建接口擴展”指南( https://www.odoo.com/documentation/10.0/howtos/web.html ),但是它太不准確或完全過時了。

在查看了諸如帳戶,項目,CRM之類的Odoo官方模塊后,我編寫了如下代碼:

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

我也是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'],

它會工作。 請嘗試一下。

您需要自行設計模板。

您會在odoo中找到用於創建模板的參考代碼。

在清單文件中將您的模板文件添加為'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模板代碼:

<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文件一起創建asset_backend.xml。

/view/assets_backend.xml->在這里您需要添加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>

我不熟悉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>

清單 .py中添加以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM