Sha256: d4d8575d2de99d43904ddfa6587507493883dd4e862f208f2600feb400898e6b

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

#= require modularity/tools/cache


# A generic ajax loader for parallel GET requests.
# Prevents duplicate requests, caches the responses
# to answer subsequent requests immediately
# without additional server requests.
#
# Warning: Caches the responses, so once a request is cached,
#          any new content on the same URL will not be visible!
class window.modularity.AjaxLoader

  constructor: ->
    @cache = new modularity.Cache()


  get: (url, callback) ->
    cached_value = @cache.get url

    # New request --> start GET call, store callback.
    unless cached_value?
      @cache.add url, [callback]
      return jQuery.get url, (data) =>
        cb(data) for cb in @cache.get(url)
        @cache.add url, data

    # Request while the GET call is still pending --> 
    # add the given callback to the list of waiting callbacks.
    if cached_value.constructor is Array
      return cached_value.push callback

    # There is a cached response for this request --> answer the request immediately.
    callback(cached_value)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
modularity-rails-0.11.1 vendor/assets/javascripts/modularity/tools/ajax_loader.coffee
modularity-rails-0.11.0 vendor/assets/javascripts/modularity/tools/ajax_loader.coffee
modularity-rails-0.10.0 vendor/assets/javascripts/modularity/tools/ajax_loader.coffee