Is there a way to call a javascript function from python function in Odoo

Viewed 284

Here is the issue I have right?

[NOTE] this is on odoo14

I have a js file that calls a function in python using rpc and it receives the return object; which is perfect.

Snippet of the JS function:

               this.rpc({
                  model: 'atm.transaction',
                  method: 'do_payment_transaction',
                  args: [customer_id],
                }).then(function (data)
                    {
                    console.log("Response of payment",data)
                });

The python function on the other hand initiates a request to a third party API; This third party API does its stuff and calls my route which I created under controllers.

I want my function that is inside my route in the controller to call a js function.[This is my main problem].

Any insights will be much appreciated.

1 Answers

Yes its totally possible. This is how for example odoo displays channel messages in front end. Its done through longpooling. Here are the step

  1. In your python function, send a notification message
self.env['bus.bus'].sendone('your_channel','your_notification_type',dict_parameters)
  1. In Javascript implement a notification listener and perform your desired action
    this.call('bus_service', 'onNotification', this, this._onLongpollingNotifications);
    /**
         * @private
         * @param {Object[]} notifications
         * @param {string} [notifications[i].type]
         * @param {string} [notifications[i].payload]
         */
        async _onLongpollingNotifications(notifications) {
// your code processed here
}

For more info check here : https://github.com/odoo/odoo/blob/ea481271f8f477d1cdcbb94826111955ed9a6a4f/addons/mail/models/mail_message.py#L750

Related