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

I develop a website and I use Newsletter module. By default, it shows success message (Thanks for subscribing!) when you click to Subscribe button. I don't want to see these toast messages for Newsletter module.


I see that this toast message comes from the code below of the website_mass_mailing.js file. 


self.displayNotification({ 
    type: toastType, 
    title: toastType === 'success' ? _t('Success') : _t('Error'), 
    message: result.toast_content, 
    sticky: true,
});


When I comment out this part, it doesn't show the toast message. But I want to do it with inheriting this file and not changing the original one. I tried to inherit it but, I couldn't succeed. Any suggestion how to do it?

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

Hi George, 

Please check with the below code snippet.

To add the changes, you'll basically have to override the existing function by monkeypatching the code.

odoo.define('your_module.name', function (require){

var publicWidget = require('web.public.widget');

publicWidget.registry.subscribe.include({
_onSubscribeClick: async function () {
var self = this;
var $email = this.$(".js_subscribe_email:visible");

if ($email.length && !$email.val().match(/.+@.+/)) {
this.$target.addClass('o_has_error').find('.form-control').addClass('is-invalid');
return false;
}
this.$target.removeClass('o_has_error').find('.form-control').removeClass('is-invalid');
let tokenObj = null;
if (this._recaptcha) {
tokenObj = await this._recaptcha.getToken('website_mass_mailing_subscribe');
if (tokenObj.error) {
self.displayNotification({
type: 'danger',
title: _t("Error"),
message: tokenObj.error,
sticky: true,
});
return false;
}
}
const params = {
'list_id': this.$target.data('list-id'),
'email': $email.length ? $email.val() : false,
};
if (this._recaptcha) {
params['recaptcha_token_response'] = tokenObj.token;
}
this._rpc({
route: '/website_mass_mailing/subscribe',
params: params,
}).then(function (result) {
let toastType = result.toast_type;
if (toastType === 'success') {
self.$(".js_subscribe_btn").addClass('d-none');
self.$(".js_subscribed_btn").removeClass('d-none');
self.$('input.js_subscribe_email').prop('disabled', !!result);
if (self.$popup.length) {
self.$popup.modal('hide');
}
}
// make the changes you need accordingly or comment out the below code.
self.displayNotification({
type: toastType,
title: toastType === 'success' ? _t('Success') : _t('Error'),
message: result.toast_content,
sticky: true,
});
});
},

})

})

Regards

Ảnh đại diện
Huỷ bỏ
Tác giả

Hi Cybrosys Techno Solutions Pvt.Ltd,

thanks for the answer. It was giving an error like this:

Traceback:
ReferenceError: _t is not defined
at http://localhost:8069/web/content/635-0ce3d45/1/web.assets_frontend_lazy.js:372:57

So I add `var _t = core._t;` before `var publicWidget` line and it worked. Appreciate!

Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 7 18
3884
2
thg 7 17
4557
0
thg 5 24
2087
1
thg 8 23
3639
1
thg 8 23
11200