Last active
December 29, 2015 19:59
-
-
Save mrloop/7720590 to your computer and use it in GitHub Desktop.
Flash Message in Ember http://coderberry.me/blog/2013/06/20/using-flash-messages-with-emberjs/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render "flash" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.CONF = | |
flash_display_time = 2000 | |
App.FlashMessage = Ember.Object.extend | |
type: 'info' | |
value: null | |
App.FlashView = Ember.View.extend | |
classNameBindings: ["className","isInfo", "isWarn", "isError" ] | |
className: 'flash' | |
isInfo: (-> | |
@get('controller.message.type') == 'info' | |
).property('controller.message.type') | |
isWarn: (-> | |
@get('controller.message.type') == 'warn' | |
).property('controller.message.type') | |
isError: (-> | |
@get('controller.message.type') == 'error' | |
).property('controller.message.type') | |
messageObserver:((sender, key, value, rev)-> | |
@fadeUp() | |
Em.run.debounce(@, @fadeDown, App.CONF.flash_display_time+10) | |
).observes('controller.message') | |
fadeTo: (opacity)-> | |
unless Em.isNone @.$() | |
@.$().animate | |
opacity: opacity | |
, 500 | |
fadeUp: -> | |
@fadeTo(1.0) | |
fadeDown: -> | |
@fadeTo(0.0) | |
App.FlashController = Ember.ObjectController.extend | |
message: null | |
init: -> | |
App.FlashQueue.addArrayObserver(@) | |
@_super() | |
willDestroy: -> | |
App.FlashQueue.removeArrayObserver(@) | |
@_super() | |
arrayWillChange: (observeObj, start, removeCount, addCount)-> | |
arrayDidChange: (observeObj, start, removeCount, addCount)-> | |
if addCount > 0 | |
@showMessages() | |
showMessages: -> | |
Ember.run.throttle(@, @showMessage, App.CONF.flash_display_time, true) | |
showMessage: -> | |
fmsg = App.FlashQueue.shiftObject() | |
unless Em.isNone(fmsg) | |
#show current message | |
@set('message', fmsg) | |
#prepare to show next message | |
Em.run.later(@, @showMessages, App.CONF.flash_display_time) | |
App.FlashQueue = Ember.ArrayProxy.create | |
content: Em.A() | |
error: (msg)-> | |
@addMessage('error', msg) | |
warn: (msg)-> | |
@addMessage('warn', msg) | |
info: (msg)-> | |
@addMessage('info', msg) | |
addMessage: (type, msg)-> | |
@pushObject(App.FlashMessage.create | |
value: msg | |
type: type | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
message.value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Call App.FlashQueue.addMessage multiple times and messages are queued up. No fade between display of queued messages.