lib/assets/javascripts/up/proxy.js.coffee in upjs-rails-0.12.5 vs lib/assets/javascripts/up/proxy.js.coffee in upjs-rails-0.13.0
- old
+ new
@@ -73,11 +73,11 @@
If the size is exceeded, the oldest items will be dropped from the cache.
@param {Number} [config.cacheExpiry=300000]
The number of milliseconds until a cache entry expires.
Defaults to 5 minutes.
@param {Number} [config.busyDelay=300]
- How long the proxy waits until emitting the `proxy:busy` [event](/up.bus).
+ How long the proxy waits until emitting the [`up:proxy:busy` event](/up:proxy:busy).
Use this to prevent flickering of spinners.
###
config = u.config
busyDelay: 300
preloadDelay: 75
@@ -96,15 +96,27 @@
size: -> config.cacheSize
expiry: -> config.cacheExpiry
key: cacheKey
log: 'up.proxy'
+
###*
@protected
@function up.proxy.get
###
- get = cache.get
+ get = (request) ->
+ request = normalizeRequest(request)
+ candidates = [request]
+ unless request.selector is 'html'
+ requestForHtml = u.merge(request, selector: 'html')
+ candidates.push(requestForHtml)
+ unless request.selector is 'body'
+ requestForBody = u.merge(request, selector: 'body')
+ candidates.push(requestForBody)
+ for candidate in candidates
+ if response = cache.get(candidate)
+ return response
###*
@protected
@function up.proxy.set
###
@@ -160,28 +172,31 @@
not be cached and the entire cache will be cleared.
Only requests with a method of `GET`, `OPTIONS` and `HEAD`
are considered to be read-only.
If a network connection is attempted, the proxy will emit
- a `proxy:load` event with the `request` as its argument.
- Once the response is received, a `proxy:receive` event will
+ a `up:proxy:load` event with the `request` as its argument.
+ Once the response is received, a `up:proxy:receive` event will
be emitted.
@function up.proxy.ajax
@param {String} request.url
@param {String} [request.method='GET']
@param {String} [request.selector]
@param {Boolean} [request.cache]
Whether to use a cached response, if available.
If set to `false` a network connection will always be attempted.
+ @param {Object} [request.headers={}]
+ An object of additional header key/value pairs to send along
+ with the request.
###
ajax = (options) ->
forceCache = (options.cache == true)
ignoreCache = (options.cache == false)
- request = u.only(options, 'url', 'method', 'data', 'selector', '_normalized')
+ request = u.only(options, 'url', 'method', 'data', 'selector', 'headers', '_normalized')
pending = true
# We don't cache non-GET responses unless `options.cache`
# is explicitly set to `true`.
@@ -202,15 +217,15 @@
# This will actually make `pendingCount` higher than the actual
# number of outstanding requests. However, we need to cover the
# following case:
#
# - User starts preloading a request.
- # This triggers *no* `proxy:busy`.
+ # This triggers *no* `up:proxy:busy`.
# - User starts loading the request (without preloading).
- # This triggers `proxy:busy`.
+ # This triggers `up:proxy:busy`.
# - The request finishes.
- # This triggers `proxy:idle`.
+ # This triggers `up:proxy:idle`.
loadStarted()
promise.always(loadEnded)
promise
@@ -218,11 +233,11 @@
###*
Returns `true` if the proxy is not currently waiting
for a request to finish. Returns `false` otherwise.
- The proxy will also emit an `proxy:idle` [event](/up.bus) if it
+ The proxy will also emit an [`up:proxy:idle` event](/up:proxy:idle) if it
used to busy, but is now idle.
@function up.proxy.idle
@return {Boolean} Whether the proxy is idle
###
@@ -231,12 +246,12 @@
###*
Returns `true` if the proxy is currently waiting
for a request to finish. Returns `false` otherwise.
- The proxy will also emit an `proxy:busy` [event](/up.bus) if it
- used to idle, but is now busy.
+ The proxy will also emit an [`up:proxy:busy` event](/up:proxy:busy) if it
+ used to be idle, but is now busy.
@function up.proxy.busy
@return {Boolean} Whether the proxy is busy
###
busy = ->
@@ -283,10 +298,10 @@
###*
This event is [emitted]/(up.emit) when [AJAX requests](/up.proxy.ajax)
have [taken long to finish](/up:proxy:busy), but have finished now.
- @event up:proxy:busy
+ @event up:proxy:idle
###
load = (request) ->
u.debug('Loading URL %o', request.url)
up.emit('up:proxy:load', request)