Skip to content

Instantly share code, notes, and snippets.

@zajca
Created June 1, 2015 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zajca/3a015e80cdb46ed7b44d to your computer and use it in GitHub Desktop.
Save zajca/3a015e80cdb46ed7b44d to your computer and use it in GitHub Desktop.
Bootstrap alers ES6 singleton class
import $ from 'jquery';
import domready from 'domready';
import is from 'is';
class Alert{
constructor(alertsContainer) {
if(!Alert.instance){
Alert.instance = this;
}else{
return Alert.instance;
}
this.container = alertsContainer;
return Alert.instance;
}
pushNewAlert(type,msg){
let html = Alert.toAlertString(type,msg);
$(this.container).append(html);
}
static toAlertString(type,msg){
return `
<div class="alert alert-${type} alert-dismissible fade in" role="alert">
<button type="button" class="close" data-dismiss="alert">
<span data-aria-hidden="true">&times;</span>
<span class="sr-only">${Translator.trans('front.close')}</span>
</button>
${msg}
</div>`;
}
}
Alert.instance = null;
Alert.TYPE = {
WARN: 'warning',
ERR: 'danger',
INFO: 'info',
OK: 'success'
};
domready(function () {
let alertsContainer = document.getElementById("flash-messages");
if (!is.nil(alertsContainer) && is.defined(alertsContainer)) {
new Alert(alertsContainer);
}
});
export default Alert;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment