Skip to content

Instantly share code, notes, and snippets.

@leepfrog
Created April 11, 2013 00:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leepfrog/5359583 to your computer and use it in GitHub Desktop.
Save leepfrog/5359583 to your computer and use it in GitHub Desktop.
CacheAdapter WIP
# Aliases
get = Ember.get
App.CacheAdapter = DS.Adapter.extend
# This defines our local storage adapter where we'll hold our cache stuff
localAdapterKlass: 'App.IDBAdapter'
# This defines our network storage adapter where we'll fetch/push/etc.. updates
networkAdapterKlass: 'DS.RESTAdapter'
# This holds on to our adapter instances
localAdapter: null
networkAdapter: null
###
All of the initial setup / configuration of local and network adapters happen here as
we are initializing the CacheAdapter
@method init
###
init: ->
@_super()
@localAdapter = get(@localAdapterKlass).create()
@networkAdapter = get(@networkAdapterKlass).create()
@_configureNetworkAdapter()
@_configureLocalAdapter()
###
This findAll method will not do many things. It's simply a passthrough to the RESTAdapter
since when calling this we probably want to load all of the remote objects from the server.
@method findAll
@param {DS.Store} store
@param {subclass of DS.Model} type
###
findAll: (store, type) -> @networkAdapter.findAll(store,type)
###
@needsTest
@private
The purpose of this method is to configure the network adapter with all of the functions
needed to make our CacheAdapter work. We are modifying the instance of the adapter so that
you theoretically could use the original adapter unmodified.
@method _configureNetworkAdapter
###
_configureNetworkAdapter: ->
###
This is used to implement the network side of the didFindAll hook. What we actually want to
do in this callback is to take our array of ids that were returned, execute a findMany against
our localAdapter, and then issue a request for the objects that we do not have locally in cache.
!!! TODO: We're doubly loading data into the store, from the networkAdapter and the localAdapter. plz fix plz.
###
@networkAdapter.didFindAll = (store, type, payload) =>
root = @rootForType(type)
plural = @pluralize(root)
ids = payload[plural]
@localAdapter.findMany(store, type, ids)
@networkAdapter.didFindMany = (store, type, payload) =>
@didFindMany(store, type, payload)
@localAdapter.updateOrCreateRecords(store, type, payload)
###
@needsTest
@private
The purpose of this method is to configure the local adapter with all of the functions
needed to make our CacheAdapter work. We are modifying the instance of the adapter so that
you theoretically could use the original adapter unmodified.
Note: Records are loaded into the store in two places. First, the valid cache is loaded. Later,
the records received via the networkAdapter are loaded.
@method _configureLocalAdapter
###
_configureLocalAdapter: ->
@localAdapter.didFindMany = (store, type, results, queryIds) =>
root = @rootForType(type)
plural = @pluralize(root)
filteredRecords = @_filterExpiredRecords(store, type, results)
resultIds = filteredRecords[plural].map (item) -> item.id
unfulfilledIds = queryIds.filter (queryId) -> resultIds.indexOf(queryId) == -1
@didFindAll(store,type,filteredRecords)
@networkAdapter.findMany(store,type,unfulfilledIds) if unfulfilledIds.length
###
@needsTest
@private
The purpose of this method is to filter out results from the localAdapter whose cache timestamp has expired
!!! Note: stub.
@method _filterExpiredRecords
###
_filterExpiredRecords: (store, type, results) ->
root = @rootForType(type)
plural = @pluralize(root)
records = results[plural]
# Implement filtering here.
results[plural] = records.filter (record) -> return true
return results
###
Used to obtain a string representation of a given model type.
This code was yanked directly from DS.RESTAdapter since it doesn't
exist inside of DS.Adapter for some reason.
@method rootForType
@param {subclass of DS.Model} type
@return {String} type name
###
rootForType: (type) -> get(this, 'serializer').rootForType(type)
###
Used to obtain a pluralized representation of a passed in string.
Same as rootForType, this code was yanked directly from DS.RESTAdapter
@method pluralize
@param {String} type name
@return {String} pluralized type name
###
pluralize: (string) -> get(this, 'serializer').pluralize(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment