Skip to content

Instantly share code, notes, and snippets.

@rubenRP
Last active February 16, 2018 12:47
Show Gist options
  • Save rubenRP/2da70b482be9d543dca36ee3043454f5 to your computer and use it in GitHub Desktop.
Save rubenRP/2da70b482be9d543dca36ee3043454f5 to your computer and use it in GitHub Desktop.
Magento 2. Medium post snippet
define([
'jquery',
'uiComponent',
'Magento_Customer/js/customer-data',
'underscore',
'jquery/jquery-storageapi'
], function ($, Component, customerData, _) {
'use strict';
return Component.extend({
defaults: {
cookieMessages: [],
messages: [],
isHidden: false,
selector: '.page.messages .messages',
listens: {
isHidden: 'onHiddenChange'
}
},
/**
* Extends Component object by storage observable messages.
*/
initialize: function () {
this._super();
this.cookieMessages = $.cookieStorage.get('mage-messages');
this.messages = customerData.get('messages').extend({
disposableCustomerData: 'messages'
});
// Force to clean obsolete messages
if (!_.isEmpty(this.messages().messages)) {
customerData.set('messages', {});
}
$.cookieStorage.set('mage-messages', '');
},
initObservable: function () {
this._super()
.observe('isHidden');
return this;
},
isVisible: function () {
return this.isHidden(!_.isEmpty(this.messages().messages) || !_.isEmpty(this.cookieMessages));
},
removeAll: function () {
$(self.selector).hide();
},
onHiddenChange: function (isHidden) {
var self = this;
// Hide message block if needed
if (isHidden) {
setTimeout(function () {
$(self.selector).hide();
}, 5000);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment