Skip to content

Instantly share code, notes, and snippets.

@joeytrapp
Last active August 29, 2015 14:05
Show Gist options
  • Save joeytrapp/65598ea84782e9ac4cfa to your computer and use it in GitHub Desktop.
Save joeytrapp/65598ea84782e9ac4cfa to your computer and use it in GitHub Desktop.
Ember Notifications
App.ApplicationController = Em.Controller.extend({
notifictions: Blocks.Notifications.create(),
actions: {
notify: function() {
if (typeof obj === 'string') {
obj = { message: obj };
}
this.get('notifications').createNotification(obj);
},
clearNotification: function() {
this.get('notifications').clearNotification(notification);
}
}
})
App.Notifications = Em.Object.extend({
active: [],
inactive: [],
timeout: 5000,
Notification: Em.Object.extend({
message: '',
type: '',
sticky: true,
willEnter: true,
didEnter: false,
willLeave: false,
didLeave: false
}),
createNotification: function(obj) {
var notification = this.get('Notification').create(obj);
this.get('active').unshiftObject(notfication);
Em.run.next(function() {
notification.setProperties({
willEnter: false,
didEnter: true
});
});
Em.run.later(this, function() {
if (!notification.get('sticky')) {
this.clearNotification(notification);
}
}, this.get('timeout'));
},
clearNotification: function(notification) {
notification.setProperties({
didEnter: false,
willLeave: true
});
Em.run.later(this, function() {
notification.setProperties({
willLeave: false,
didLeave: true
});
this.get('active').removeObject(notification);
this.get('inactive').unshiftObject(notification);
}, 2000);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment