Inject storage and sessionStorage

This commit is contained in:
Piotr Sarnacki 2015-01-19 17:20:43 +01:00
parent 547620648d
commit b06582557a
2 changed files with 44 additions and 29 deletions

View File

@ -10,9 +10,9 @@ window.Auth = Ember.Object.extend
).property(),
signOut: ->
Travis.storage.removeItem('travis.user')
Travis.storage.removeItem('travis.token')
Travis.sessionStorage.clear()
@storage.removeItem('travis.user')
@storage.removeItem('travis.token')
@sessionStorage.clear()
@set('state', 'signed-out')
@set('user', undefined)
if user = @get('currentUser')
@ -29,7 +29,7 @@ window.Auth = Ember.Object.extend
$('<iframe id="auth-frame" />').hide().appendTo('body').attr('src', url)
autoSignIn: (data) ->
data ||= @userDataFrom(Travis.sessionStorage) || @userDataFrom(Travis.storage)
data ||= @userDataFrom(@sessionStorage) || @userDataFrom(@storage)
@setData(data) if data
userDataFrom: (storage) ->
@ -47,7 +47,7 @@ window.Auth = Ember.Object.extend
validateUser: (user) ->
fieldsToValidate = ['id', 'login', 'token', 'correct_scopes']
if Travis.config.pro
if @config.pro
fieldsToValidate.push 'channels'
fieldsToValidate.every( (field) => @validateHas(field, user) ) && user.correct_scopes
@ -60,8 +60,8 @@ window.Auth = Ember.Object.extend
false
setData: (data) ->
@storeData(data, Travis.sessionStorage)
@storeData(data, Travis.storage) unless @userDataFrom(Travis.storage)
@storeData(data, @sessionStorage)
@storeData(data, @storage) unless @userDataFrom(@storage)
user = @loadUser(data.user)
@set('currentUser', user)
@ -76,8 +76,8 @@ window.Auth = Ember.Object.extend
# if user is still signed in, update saved data
if @get('signedIn')
data.user.token = user.token
@storeData(data, Travis.sessionStorage)
@storeData(data, Travis.storage)
@storeData(data, @sessionStorage)
@storeData(data, @storage)
, (status, xhr) =>
@signOut() if status == 403

View File

@ -6,7 +6,6 @@ require 'app'
window.ENV ||= {}
window.ENV.RAISE_ON_DEPRECATION = true
# TODO: how can I put it in Travis namespace and use immediately?
Storage = Em.Object.extend
init: ->
@set('storage', {})
@ -131,28 +130,43 @@ $.extend Travis,
INTERVALS: { times: -1, updateTimes: 1000 }
storage: (->
storage = null
try
storage = window.localStorage || throw('no storage')
catch err
storage = Storage.create()
sessionStorage = (->
storage = null
try
# firefox will not throw error on access for sessionStorage var,
# you need to actually get something from session
sessionStorage.getItem('foo')
storage = sessionStorage
catch err
storage = Storage.create()
storage
)()
storage
)()
sessionStorage: (->
storage = null
try
# firefox will not throw error on access for sessionStorage var,
# you need to actually get something from session
sessionStorage.getItem('foo')
storage = sessionStorage
catch err
storage = Storage.create()
storage = (->
storage = null
try
storage = window.localStorage || throw('no storage')
catch err
storage = Storage.create()
storage
)()
storage
)()
Travis.initializer
name: 'storage'
initialize: (container, application) ->
application.register 'storage:main', storage, { instantiate: false }
application.register 'sessionStorage:main', sessionStorage, { instantiate: false }
application.inject('auth', 'storage', 'storage:main')
application.inject('auth', 'sessionStorage', 'sessionStorage:main')
# I still use Travis.storage in some places which are not that easy to
# refactor
application.storage = storage
application.sessionStorage = sessionStorage
Travis.initializer
name: 'googleAnalytics'
@ -177,6 +191,7 @@ Travis.initializer
application.inject('controller', 'config', 'config:main')
application.inject('route', 'config', 'config:main')
application.inject('auth', 'config', 'config:main')
Travis.initializer
name: 'inject-pusher'