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

I am trying to implement a component which displays in Systray currently running task in timer.timer model (Odoo 15 Enterprise)

I've extended the model to send a payload to trigger an action in the component


class TimerTimer(models.Model):
  _inherit = 'timer.timer'

  def create(self, vals):
    record = super(TimerTimer, self).create(vals)
    self.env['bus.bus']._sendone(record.user_id.partner_id, "timer.timer/updated", {})
    return record

  def write(self, vals):
    record = super(TimerTimer, self).write(vals)
    self.env['bus.bus']._sendone(self.user_id.partner_id, "timer.timer/updated", {})
    return record

I can see the payload incoming to browser via poll request. Now, how to subscribe to it in my component? I've tried this:


class Timer extends Component {
    setup() {
        let self = this
        console.log("EventBus")
        console.log(self\.env\.bus\)
\ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \  \    \   useBus\(self.env.bus, "timer.timer/updated", event => {
            console.log(event)
        })
}
Timer.template = "eaxpl_timesheet_timer.Timer"
registry.category("systray").add("eaxpl_timesheet_timer.Timer", { Component: Timer }, { sequence: 3 });

export default Timer

Unfortunately it does not seem to work. The event bus object is there but it does not seem to catch the payload incoming. I have tried listening on:

  • timer.timer/updated
  • notification

Any hints appreciated


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

Finally i got it, after 3 days of work and research..


In front end for example for create a new custom channel
/** @odoo-module **/
// BusListener.js
import { registry } from"@web/core/registry";
constmyService= {    dependencies: ['bus_service'],    start(_, { bus_service }) {        console.log('Add notification')
        bus_service.addChannel('my-event') // New channel
        bus_service.addEventListener('notification', ({ detail: notifications }) => {            console.log({ notifications })            for (const { payload, type } ofnotifications) {                if (type==="my-event") {                    this.myEventHandler(payload)                }            }        });
        bus_service.addEventListener('connect', () => {            console.log('Connected')        });
        bus_service.start();
    },
    myEventHandler(event) {        console.log({ event });    }};
registry.category("services").add("myService", myService);
In backend:

# -*- coding: utf-8 -*-fromodooimportmodels, fields, api
classBusModel(models.Model):    _name='bus_test'    _description="Bus para my modulo"
    name=fields.Char(string="Name")
    @api.model    defmy_method(self):        self.env['bus.bus']._sendone('my-event', 'my-event', {            'server_version': "my-event"        })
Ảnh đại diện
Huỷ bỏ

Odoo forum for developers the forum can't paste a code of developers, it is a shit of post system

Câu trả lời hay nhất

setup() {

this.env.bus.on('WEB_CLIENT_READY', null, async () => {

const bus_service = owl.Component.env.services.bus_service;

bus_service.onNotification(this, this._onNotification);

bus_service.addChannel(/* record.user_id.partner_id */);

bus_service.startPolling();

});

}

Sorry for formatting, this web editor is terrible. Grep 15.0 for plenty of examples of what _onNotification looks like.

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

did you ever find an answer?

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
4
thg 4 25
4000
0
thg 6 24
1111
1
thg 12 23
2345
0
thg 6 24
1435
2
thg 5 24
1380