Skip to content

Instantly share code, notes, and snippets.

@poteto
Last active March 7, 2016 12:06
Show Gist options
  • Save poteto/366002c9f0ae1c044dad to your computer and use it in GitHub Desktop.
Save poteto/366002c9f0ae1c044dad to your computer and use it in GitHub Desktop.
import Ember from 'ember';
import FlashMessage from 'ember-cli-flash/flash/object';
var {
computed,
get,
run
} = Ember;
export default Ember.Object.extend({
queue : Ember.A([]),
isEmpty : computed.equal('queue.length', 0),
defaultTimeout : 2000,
success(message, timeout=get(this, 'defaultTimeout')) {
this._add(message, 'success', timeout);
},
info(message, timeout=get(this, 'defaultTimeout')) {
this._add(message, 'info', timeout);
},
warning(message, timeout=get(this, 'defaultTimeout')) {
this._add(message, 'warning', timeout);
},
danger(message, timeout=get(this, 'defaultTimeout')) {
this._add(message, 'danger', timeout);
},
addMessage(message, type='info', timeout=get(this, 'defaultTimeout')) {
this._add(message, type, timeout);
},
clearMessages() {
let flashes = get(this, 'queue');
run.next(this, () => {
flashes.clear();
});
},
// private
_add(message, type, timeout) {
let flashes = get(this, 'queue');
let flash = this._newFlashMessage(this, message, type, timeout);
run.next(this, () => {
flashes.pushObject(flash);
});
},
_newFlashMessage(service, message, type, timeout) {
Ember.assert('Must pass a valid flash service', service);
Ember.assert('Must pass a valid flash message', message);
type = (typeof type === 'undefined') ? 'info' : type;
timeout = timeout || Ember.get(this, 'defaultTimeout');
return FlashMessage.create({
type : type,
message : message,
timeout : timeout,
flashService : service
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment