ducky/web

Paddy 2015-06-30 Parent:99a43a6d1d30 Child:bc1478742a50

14:275a83e4c02e Go to Latest

ducky/web/src/main.js

Persist session data to localStorage. Create a helper library that figures out whether to write to chrome.storage.local or window.localStorage, and unifies their two APIs. Update the Me model to use the getOrFetch method for the profiles collection when retrieving the user's profile. This, unfortunately, makes it an async call (because we may need to fetch data from the server), so we can no longer have it be a derived property, which is a shame. It instead must just be the me.profile() function. Separate out the logic to determine when an access token expires, and turn it into the tokenExpires function. Fill the writeToCache placeholder with the logic to store the current session in either window.localStorage or chrome.storage.local, whichever is the more appropriate, using the helper library. Create the load helper function that will attempt to read session data from localStorage or chrome.storage.local, whichever the library decides is available, and updates the session based on it. Implement the logout function, which just uses the helper library to remove the session data from window.localStorage or chrome.storage.local. We should also be resetting the app.me variable, however. Create a debouncedWriteToCache function that will only write to the cache once every 250 ms, to avoid rushes on the cache. When instantiating our app.me variable, load it in from localStorage or chrome.storage.local if we can. Also, listen for changes to app.me, and persist them to chrome.storage.local or localStorage.

History
paddy@0 1 import app from 'ampersand-app'
paddy@0 2 import Router from './router'
paddy@0 3 import Profiles from './models/profiles'
paddy@0 4 import Me from './models/me'
paddy@0 5 import normalize from 'normalize.css/normalize.css'
paddy@0 6 import styles from './styles/main.scss'
paddy@0 7
paddy@0 8 window.app = app.extend({
paddy@0 9 init () {
paddy@0 10 this.profiles = new Profiles()
paddy@14 11 this.me = new Me().load()
paddy@14 12 this.me.on('change', this.me.debouncedWriteToCache)
paddy@0 13 this.router = new Router()
paddy@0 14 this.router.history.start({ pushState: true })
paddy@0 15 }
paddy@0 16 })
paddy@0 17
paddy@0 18 app.init()