Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
2988 Lượt xem

hi 

 

I took the example from the official docs for js widget : 

 

name of module : learn_js  / odoo 16  community 

 

///////////////in learn_js/static/src/js/counter_widget.js

 

odoo.define('learn_js.counter_widget',function(require){

 

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

 

var Counter = Widget.extend({

    template: 'some.template',

    events: {

        'click button': '_onClick',

    },

    init: function (parent, value) {

        this._super(parent);

        this.count = value;

    },

    _onClick: function () {

        this.count++;

        this.$('.val').text(this.count);

    },

 

});

 

    // Create the instance

var counter = new Counter(this, 4);

// Render and insert into DOM

counter.appendTo(".some-div");

 

})

////////////////////////// in learn_js/static/src/xml/counter_templates.xml

   

   

also trying this  and not working : 

 

       

   

   

       

 

////////////// manifest :

{

'name': 'learn js',

    'version': '1.1',

    'category': '',

    'depends': ['base'],

    'data': [

# 'views/nherit_model.xml',

'views/normal_view.xml',

        'security/ir.model.access.csv',

    ],

'assets': {

'web.assets_backend': ['/learn_js/static/src/js/counter_widget.js'],

    'web.assets_qweb': ['/learn_js/static/src/xml/counter_templates.xml']

},

    'application': True,

}

///////////// form view

 

 

//////////////////

And when I apply it gives me this error :

 

UncaughtPromiseError

Uncaught Promise > QWeb2: Template 'some.template' not found

Error: QWeb2: Template 'some.template' not found

    at Object.exception (http://localhost:8069/web/assets/1206-68485b7/web.assets_common.min.js:5526:7)

    at Engine._render (http://localhost:8069/web/assets/1206-68485b7/web.assets_common.min.js:5570:103)

    at Engine.render (http://localhost:8069/web/assets/1206-68485b7/web.assets_common.min.js:5566:151)

    at Class.renderElement (http://localhost:8069/web/assets/1206-68485b7/web.assets_common.min.js:6081:775)

    at http://localhost:8069/web/assets/1206-68485b7/web.assets_common.min.js:6091:6

///////////////////

So I want to know how to create this counter using the widget

and add it to the form view in this element in the same way.

Thanks

 


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

try this way and modify your code:

odoo.define('learn_js.counter_widget', function (require) {
var Widget = require('web.Widget');

var Counter = Widget.extend({
template: 'learn_js.counter_template', // Update the template name

events: {
'click button': '_onClick',
},

init: function (parent, value) {
this._super(parent);
this.count = value;
},

_onClick: function () {
this.count++;
this.$('.val').text(this.count);
},
});

var counter = new Counter(this, 4);
counter.appendTo(".some-div");
});

  1. Create the template in counter_templates.xml:

templates>
t t-name="learn_js.counter_template">
div>
button>Increment
span class="val">4
/div>
/t>
/templates>

  1. Update the manifest file (__manifest__.py) to include the template XML file:

{
'name': 'learn js',
'version': '1.1',
'category': '',
'depends': ['base'],
'data': [
'views/normal_view.xml',
'security/ir.model.access.csv',
'static/src/xml/counter_templates.xml', // Add this line
],
'assets': {
'web.assets_backend': [
'static/src/js/counter_widget.js',
],
},
'application': True,
}

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 5 23
7193
2
thg 12 20
10612
1
thg 7 16
4055
0
thg 12 23
1875
0
thg 1 21
2777