Refactored storage module.

This commit is contained in:
Scott Erickson 2014-01-26 14:44:08 -08:00
parent 47c78d1758
commit ae2bb625c8
4 changed files with 18 additions and 16 deletions

View file

@ -1,7 +1,7 @@
CocoClass = require 'lib/CocoClass' CocoClass = require 'lib/CocoClass'
{me, CURRENT_USER_KEY} = require 'lib/auth' {me, CURRENT_USER_KEY} = require 'lib/auth'
{backboneFailure} = require 'lib/errors' {backboneFailure} = require 'lib/errors'
{saveObjectToStorage} = require 'lib/storage' storage = require 'lib/storage'
# facebook user object props to # facebook user object props to
userPropsToSave = userPropsToSave =
@ -59,7 +59,7 @@ module.exports = FacebookHandler = class FacebookHandler extends CocoClass
error: backboneFailure, error: backboneFailure,
url: "/db/user?facebookID=#{r.id}&facebookAccessToken=#{@authResponse.accessToken}" url: "/db/user?facebookID=#{r.id}&facebookAccessToken=#{@authResponse.accessToken}"
success: (model) -> success: (model) ->
saveObjectToStorage(CURRENT_USER_KEY, model.attributes) storage.save(CURRENT_USER_KEY, model.attributes)
window.location.reload() if model.get('email') isnt oldEmail window.location.reload() if model.get('email') isnt oldEmail
}) })

View file

@ -1,7 +1,7 @@
CocoClass = require 'lib/CocoClass' CocoClass = require 'lib/CocoClass'
{me, CURRENT_USER_KEY} = require 'lib/auth' {me, CURRENT_USER_KEY} = require 'lib/auth'
{backboneFailure} = require 'lib/errors' {backboneFailure} = require 'lib/errors'
{saveObjectToStorage} = require 'lib/storage' storage = require 'lib/storage'
# gplus user object props to # gplus user object props to
userPropsToSave = userPropsToSave =
@ -73,7 +73,7 @@ module.exports = GPlusHandler = class GPlusHandler extends CocoClass
error: backboneFailure, error: backboneFailure,
url: "/db/user?gplusID=#{gplusID}&gplusAccessToken=#{@accessToken}" url: "/db/user?gplusID=#{gplusID}&gplusAccessToken=#{@accessToken}"
success: (model) -> success: (model) ->
saveObjectToStorage(CURRENT_USER_KEY, model.attributes) storage.save(CURRENT_USER_KEY, model.attributes)
window.location.reload() window.location.reload()
}) })

View file

@ -1,6 +1,6 @@
{backboneFailure, genericFailure} = require 'lib/errors' {backboneFailure, genericFailure} = require 'lib/errors'
User = require 'models/User' User = require 'models/User'
{saveObjectToStorage, loadObjectFromStorage} = require 'lib/storage' storage = require 'lib/storage'
module.exports.CURRENT_USER_KEY = CURRENT_USER_KEY = 'whoami' module.exports.CURRENT_USER_KEY = CURRENT_USER_KEY = 'whoami'
BEEN_HERE_BEFORE_KEY = 'beenHereBefore' BEEN_HERE_BEFORE_KEY = 'beenHereBefore'
@ -10,7 +10,7 @@ module.exports.createUser = (userObject, failure=backboneFailure) ->
user.save({}, { user.save({}, {
error: failure, error: failure,
success: (model) -> success: (model) ->
saveObjectToStorage(CURRENT_USER_KEY, model) storage.save(CURRENT_USER_KEY, model)
window.location.reload() window.location.reload()
}) })
@ -21,7 +21,7 @@ module.exports.loginUser = (userObject, failure=genericFailure) ->
password:userObject.password password:userObject.password
}, },
(model) -> (model) ->
saveObjectToStorage(CURRENT_USER_KEY, model) storage.save(CURRENT_USER_KEY, model)
window.location.reload() window.location.reload()
) )
jqxhr.fail(failure) jqxhr.fail(failure)
@ -29,7 +29,7 @@ module.exports.loginUser = (userObject, failure=genericFailure) ->
module.exports.logoutUser = -> module.exports.logoutUser = ->
FB?.logout?() FB?.logout?()
res = $.post('/auth/logout', {}, -> res = $.post('/auth/logout', {}, ->
saveObjectToStorage(CURRENT_USER_KEY, null) storage.save(CURRENT_USER_KEY, null)
window.location.reload() window.location.reload()
) )
res.fail(genericFailure) res.fail(genericFailure)
@ -38,7 +38,7 @@ init = ->
# Load the user from local storage, and refresh it from the server. # Load the user from local storage, and refresh it from the server.
# Also refresh and cache the gravatar info. # Also refresh and cache the gravatar info.
storedUser = loadObjectFromStorage(CURRENT_USER_KEY) storedUser = storage.load(CURRENT_USER_KEY)
firstTime = not storedUser firstTime = not storedUser
module.exports.me = window.me = new User(storedUser) module.exports.me = window.me = new User(storedUser)
me.url = -> '/auth/whoami' me.url = -> '/auth/whoami'
@ -50,14 +50,14 @@ init = ->
# Assign testGroupNumber to returning visitors; new ones in server/handlers/user # Assign testGroupNumber to returning visitors; new ones in server/handlers/user
me.set 'testGroupNumber', Math.floor(Math.random() * 256) me.set 'testGroupNumber', Math.floor(Math.random() * 256)
me.save() me.save()
saveObjectToStorage(CURRENT_USER_KEY, me.attributes) storage.save(CURRENT_USER_KEY, me.attributes)
me.loadGravatarProfile() if me.get('email') me.loadGravatarProfile() if me.get('email')
me.on('sync', userSynced) me.on('sync', userSynced)
userSynced = (user) -> userSynced = (user) ->
Backbone.Mediator.publish('me:synced', {me:user}) Backbone.Mediator.publish('me:synced', {me:user})
saveObjectToStorage(CURRENT_USER_KEY, user) storage.save(CURRENT_USER_KEY, user)
init() init()
@ -71,7 +71,7 @@ Backbone.Mediator.subscribe('level-set-volume', onSetVolume, module.exports)
trackFirstArrival = -> trackFirstArrival = ->
# will have to filter out users who log in with existing accounts separately # will have to filter out users who log in with existing accounts separately
# but can at least not track logouts as first arrivals using local storage # but can at least not track logouts as first arrivals using local storage
beenHereBefore = loadObjectFromStorage(BEEN_HERE_BEFORE_KEY) beenHereBefore = storage.load(BEEN_HERE_BEFORE_KEY)
return if beenHereBefore return if beenHereBefore
window.tracker?.trackEvent 'First Arrived' window.tracker?.trackEvent 'First Arrived'
saveObjectToStorage(BEEN_HERE_BEFORE_KEY, true) storage.save(BEEN_HERE_BEFORE_KEY, true)

View file

@ -1,4 +1,4 @@
module.exports.loadObjectFromStorage = (key) -> module.exports.load = (key) ->
s = localStorage.getItem(key) s = localStorage.getItem(key)
return null unless s return null unless s
try try
@ -8,6 +8,8 @@ module.exports.loadObjectFromStorage = (key) ->
console.warning('error loading from storage', key) console.warning('error loading from storage', key)
return null return null
module.exports.saveObjectToStorage = (key, value) -> module.exports.save = (key, value) ->
s = JSON.stringify(value) s = JSON.stringify(value)
localStorage.setItem(key, s) localStorage.setItem(key, s)
module.exports.remove = (key) -> localStorage.removeItem key