Hi community
I am trying to find out how the useBus hook works. It has changed from previous versions and there seems to be no examples anywhere that are up to date. Using the awesome_owl framework I have the modified the world-renowned counter component as follows:
/** @odoo-module */
// counter.js
import { Component, useState } from "@odoo/owl";
import { useBus } from "@web/core/utils/hooks";
export class Counter extends Component {
static template = "awesome_owl.Counter";
setup() {
this.state = useState({ value: 1 });
useBus(this.env.bus, "some-event", event => {
console.log(event);
});
}
increment() {
//this.state.value = this.state.value + 1;
this.callApi(this.state.value);
}
callApi(val) {
window.location.replace("/test?state=" + val);
}
}
Counter:
style="font-family:'console' width: 100%; height: 600px; overflow-y: auto;color:rgb(255,255,255); background: black;"
readonly="1">
and in the controllers.py, I have added a new route
@http.route(['/test'], type='http', auth='public')
def call_incrementor(self, **args):
_logger.info('**************************************************************************************')
_logger.info('Controller called. state=' + args["state"] )
count = args["state"] + 1
channel = "counter_channel"
rtn_data = {'new_state': count}
message = {
"data": rtn_data,
"channel": channel
}
request.env["bus.bus"]._sendone(channel, "notification", message)
return {'result': 'Live data sent successfully'}
//return request.render('awesome_owl.playground') #added temporarily to complete the loop
The starts are added so I can find the output in the log easily.
So I have 2 problems (questions)
- Since you can no longer add a channel to the bus service/hook, how to get the results to the frontend, and
- I get an error immediately when using the useBus Hook as follows
Error: The following error occurred in onMounted: "Cannot read properties of undefined (reading 'addEventListener')"
at wrapError (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:10189:23)
at onMounted (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:10242:27)
at useEffect (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:13484:9)
at useBus (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:39138:5)
at Counter.setup (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:42321:9)
at new ComponentNode (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:9941:28)
at http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:13345:28
at Playground.template (eval at compile (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:13116:20), :12:16)
at MountFiber._render (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:9306:38)
at MountFiber.render (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:9298:18)
Caused by: TypeError: Cannot read properties of undefined (reading 'addEventListener')
at http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:39141:17
at Counter. (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:13486:23)
at http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:10204:32
at MountFiber.complete (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:9420:29)
at Scheduler.processFiber (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:13199:27)
at Scheduler.processTasks (http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:13175:22)
at http://192.168.0.10:8069/web/assets/debug/awesome_owl.assets_playground.js:13165:68
logError @ main.js:37
Your help would be much appreciated
Ok my xml didn't display well at all but it is unchanged from the original