refactor broadcasts

This commit is contained in:
Sven Fuchs 2012-10-19 04:27:07 +02:00
parent 2df91c296c
commit a806dd7ff8
9 changed files with 1648 additions and 1635 deletions

View File

@ -31,7 +31,8 @@
@signIn() if localStorage.getItem('travis.auto_signin')
signOut: ->
localStorage.clear()
localStorage.removeItem('travis.auto_signin')
localStorage.removeItem('travis.locale')
sessionStorage.clear()
@setData()

View File

@ -2,37 +2,32 @@ Travis.FlashController = Ember.ArrayController.extend
broadcastBinding: 'Travis.app.currentUser.broadcasts'
init: ->
@set('flashes', Ember.A())
@_super.apply this, arguments
@set('content', Ember.A())
content: (->
@get('unseenBroadcasts').concat(@get('flashes'))
).property('unseenBroadcasts.length', 'flashes.length')
unseenBroadcasts: (->
@get('broadcasts').filterProperty('isSeen', false)
).property('broadcasts.isLoaded', 'broadcasts.length')
broadcasts: (->
Travis.Broadcast.find() if Travis.app.get('currentUser')
if Travis.app.get('currentUser') then Travis.Broadcast.find() else Ember.A()
).property('Travis.app.currentUser')
broadcastsObserver: (->
if broadcasts = @get('broadcasts')
broadcasts.forEach (msg) => @pushObject(msg.toObject()) unless @isSeenBroadcast(msg)
).observes('broadcasts.length')
loadFlashes: (msgs) ->
for msg in msgs
type = Ember.keys(msg)[0]
msg = { type: type, message: msg[type] }
@pushObject(msg)
Ember.run.later(this, (-> @removeObject(msg)), 15000)
@get('flashes').pushObject(msg)
Ember.run.later(this, (-> @get('flashes').removeObject(msg)), 15000)
close: (msg) ->
@storeSeenBroadcast(msg) if msg.type == 'broadcast'
@removeObject(msg)
if msg instanceof Travis.Broadcast
msg.setSeen()
@notifyPropertyChange('unseenBroadcasts')
else
@get('flashes').removeObject(msg)
isSeenBroadcast: (msg) ->
msg.get('id') in @seenBroadcasts()
seenBroadcasts: ->
seen = localStorage.getItem('travis.seen_broadcasts')
if seen then JSON.parse(seen) else []
storeSeenBroadcast: (msg) ->
seen = @seenBroadcasts()
seen.push(msg.id)
localStorage.setItem('travis.seen_broadcasts', JSON.stringify(seen))

View File

@ -6,6 +6,18 @@ require 'travis/model'
toObject: ->
{ type: 'broadcast', id: @get('id'), message: @get('message') }
isSeen: (->
@get('id') in Travis.Broadcast.seen
).property()
setSeen: ->
Travis.Broadcast.seen.pushObject(@get('id'))
localStorage.setItem('travis.seen_broadcasts', JSON.stringify(Travis.Broadcast.seen))
@notifyPropertyChange('isSeen')
@Travis.Broadcast.reopenClass
seen: Ember.A(JSON.parse(localStorage.getItem('travis.seen_broadcasts')) || [])
# TODO fix or monkey-patch the adapter's url and key lookup/generation crap
# url: 'users/broadcasts'

View File

@ -6,7 +6,11 @@
FlashItemView: Travis.View.extend
tagName: 'li'
classNameBindings: ['flash.type']
classNameBindings: ['type']
type: (->
@get('flash.type') || 'broadcast'
).property('flash.type')
close: (event) ->
@get('controller').close(@get('flash'))

View File

@ -38,12 +38,12 @@ require 'ext/ember/namespace'
return unless locale
I18n.locale = locale
localStorage.setItem('travis.config.locale', locale)
localStorage.setItem('travis.locale', locale)
run: (attrs) ->
location.href = location.href.replace('#!/', '') if location.hash.slice(0, 2) == '#!'
@setLocale localStorage.getItem('travis.config.locale')
@setLocale localStorage.getItem('travis.locale')
Ember.run.next this, ->
app = Travis.App.create(attrs || {})

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
e4c25d9c
392e058e