Skip to content

Instantly share code, notes, and snippets.

@mrloop
Last active December 29, 2015 19:59
Show Gist options
  • Save mrloop/7720590 to your computer and use it in GitHub Desktop.
Save mrloop/7720590 to your computer and use it in GitHub Desktop.
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
)
message.value
@mrloop
Copy link
Author

mrloop commented Nov 30, 2013

Call App.FlashQueue.addMessage multiple times and messages are queued up. No fade between display of queued messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment