Sha256: f3d3c5c31e4347b93bff48b4fb7c035db9edad1be6ea71df997a6d26221c12e5

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

#= require ../resources

Joosy.Modules.Resources.Cacher =

  included: ->
    @cache   = (cacheKey) -> @::__cacheKey = cacheKey
    @fetcher = (fetcher) -> @::__fetcher = fetcher

    @cached = (callback, cacheKey=false, fetcher=false) ->
      if typeof(cacheKey) == 'function'
        fetcher  = cacheKey
        cacheKey = undefined

      cacheKey ||= @::__cacheKey
      fetcher  ||= @::__fetcher

      if cacheKey && localStorage && localStorage[cacheKey]
        instance = @build JSON.parse(localStorage[cacheKey])...
        callback? instance
        instance.refresh()
      else
        @fetch (results) =>
          instance = @build results...
          callback? instance

    @fetch = (callback) ->
      @::__fetcher (results...) =>
        localStorage[@::__cacheKey] = JSON.stringify(results) if @::__cacheKey && localStorage
        callback results

  refresh: (callback) ->
    @constructor.fetch (results) =>
      @load results...
      callback? @

# AMD wrapper
if define?.amd?
  define 'joosy/modules/resources/cacher', -> Joosy.Modules.Resources.Cacher

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
joosy-1.2.0.beta.2 source/joosy/modules/resources/cacher.coffee