Sha256: 318aaa23096cd811f143d27ad4b3626a34383009228de6ffaa272000477e8009

Contents?: true

Size: 947 Bytes

Versions: 2

Compression:

Stored size: 947 Bytes

Contents

# 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!
Module.loader =

  cache: {}

  get: (url, callback) ->
    cache = Module.loader.cache[url]

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

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
modularity-rails-0.8.1 vendor/assets/javascripts/tools/ajax_loader.coffee
modularity-rails-0.8.0 vendor/assets/javascripts/tools/ajax_loader.coffee