Skip to content

Instantly share code, notes, and snippets.

@mrinterweb
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrinterweb/2642c26d4b0ea0ff819b to your computer and use it in GitHub Desktop.
Save mrinterweb/2642c26d4b0ea0ff819b to your computer and use it in GitHub Desktop.
App.FriendCache = Em.Object.extend
init: ->
@store = App.__container__.lookup('store:main')
findAll: ->
localforage.getItem('friends').then (cfriends)=>
if Em.isEmpty(cfriends)
@findAndCache()
else
allFriends = @store.all('friend')
if Em.isEmpty allFriends
cfriends.map (cfriend)=>
@store.createRecord('friend', cfriend)
else
allFriends
findAndCache: ->
@store.find('friend').then (friends)->
cfriends = friends.map (friend)->
if friend.id
s = friend.serialize()
s.id = friend.id
s.group_ids = friend._data.groups.mapBy('id')
s
localforage.setItem('friends', cfriends.compact())
friends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment