From 23bca39c7ee1e743c706ea085f605caacb6fe576 Mon Sep 17 00:00:00 2001 From: Philip Klauzinski Date: Wed, 15 Apr 2015 19:26:28 -0500 Subject: [PATCH] Modified `_cache.response` object to use the `api.url` plus the serialized request string as a key to accommodate caching form requests to the same URL but with different input data. --- bower.json | 2 +- package.json | 2 +- payload.js | 14 +++++++------- payload.min.js | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/bower.json b/bower.json index b017f6e..939f106 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "payload.js", - "version": "0.1.2", + "version": "0.1.3", "homepage": "https://github.com/pklauzinski/payload", "authors": [ { diff --git a/package.json b/package.json index 10f9bba..9f72163 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "payload.js", - "version": "0.1.2", + "version": "0.1.3", "description": "HTML5-data-driven behavioral layer designed to interact intuitively with REST APIs from within SPAs (single-page-applications) and mobile apps.", "main": "payload.js", "scripts": { diff --git a/payload.js b/payload.js index 8a3915c..b0bfb03 100644 --- a/payload.js +++ b/payload.js @@ -196,7 +196,7 @@ if (typeof(opts) === 'function') { _options.apiCallback = opts; } else if (typeof(opts) === 'object') { - $.extend(_options, opts); + _this.merge(opts); } else if (typeof(opts) === 'string') { _options.context = opts; } @@ -204,8 +204,7 @@ }; this.merge = function(opts) { - $.extend(_options, opts); - return this; + return $.extend(_options, opts); }; this.apiRequest = function($this) { @@ -236,6 +235,7 @@ } }, templateName = $this.attr(_dataPrefix + 'template') || $this.attr(_dataPrefix + 'partial'), + requestKey = api.url + $this.serialize(), $selector, $loading, $load, html, templateData, params; // Add the request payload to the template data under "request" namespace @@ -285,12 +285,12 @@ _cacheView($this, api, templateName); - if (api.cacheResponse && _cache.response[api.url] && _cache.response[api.url].data && _cache.response[api.url].done) { - templateData = $.extend({}, _cache.response[api.url].data, api.templateData); + if (api.cacheResponse && _cache.response[requestKey] && _cache.response[requestKey].data && _cache.response[requestKey].done) { + templateData = $.extend({}, _cache.response[requestKey].data, api.templateData); html = api.template ? api.template(templateData) : api.partial(templateData); $selector.html(html); _options.apiAfterRender(params); - _cache.response[api.url].done(); + _cache.response[requestKey].done(); publishEvents([params]); return; } @@ -360,7 +360,7 @@ } if (api.cacheResponse) { - _cache.response[api.url] = { + _cache.response[requestKey] = { data: templateData, done: xhrDone }; diff --git a/payload.min.js b/payload.min.js index a48409d..5ebc456 100644 --- a/payload.min.js +++ b/payload.min.js @@ -6,8 +6,8 @@ * http://www.opensource.org/licenses/mit-license.php * * @author Philip Klauzinski - * @version 0.1.2 + * @version 0.1.3 * @requires jQuery v1.7+ * @preserve */ -!function(a,b){"use strict";"function"==typeof define&&define.amd?define(["jquery"],b):"object"==typeof exports?module.exports=b(require("jquery")):a.Payload=b(a.jQuery)}(this,function(a){"use strict";var b=function(){var b,c,d=this,e={apiCallback:a.noop,apiOnClick:function(){return!0},apiOnSubmit:function(){return!0},apiAfterRender:a.noop,apiAccessToken:!1,apiResponseParent:!1,context:document.body,dataNamespace:!1,debug:!1,loadingHtml:"Loading...",useHref:!1,xhrAlways:a.noop,xhrBeforeSend:a.noop,xhrDone:a.noop,xhrFail:a.noop},f="data-"+(e.dataNamespace?a.trim(e.dataNamespace)+"-":""),g={API_LINK:"a["+f+"selector],a["+f+"url],button["+f+"selector],button["+f+"url]",API_FORM:"form["+f+"selector],form["+f+"url]",AUTO_LOAD:"["+f+'auto-load="true"]',CLICK:"["+f+"click]",LOADING:"["+f+'role="loading"]'},h={response:{},view:{}},i=a({}),j=function(a){var b,c;if(e.debug&&"object"==typeof console&&("object"==typeof a||"function"==typeof console[a]))if("object"==typeof a)for(c in a)"function"==typeof console[c]?(b="string"==typeof a[c]||"object"==typeof a[c]&&void 0===a[c].length?[a[c]]:a[c],console[c].apply(console,b)):console.log(a[c]);else console[a].apply(console,Array.prototype.slice.call(arguments,1))},k=function(a){throw"Payload.js: "+a},l=function(){return b=a(e.context),b.length||k('Selector "'+e.context+'" not found'),m(),d},m=function(){n(),o()},n=function(){b.on("click.api-request auto-load.api-request",g.API_LINK,function(b){var c=a(this);b.preventDefault(),c.prop("disabled")||c.hasClass("disabled")||c.data("disabled")||e.apiOnClick(c,b)&&d.apiRequest(c)}).on("submit.api-request",g.API_FORM,function(b){var c=a(this);b.preventDefault(),e.apiOnSubmit(c,b)&&d.apiRequest(c)})},o=function(){b.on("click.click",g.CLICK,function(b){var c=a(this),d=a(c.attr(f+"click"));b.preventDefault(),d.click()})},p=function(b,d,e){var f,g,i=d.selector,j=a(i);if(!b.closest(i).length){if(h.view[i]){for(f in h.view[i])if(h.view[i].hasOwnProperty(f)&&null===h.view[i][f].html){h.view[i][f].html=j.contents().detach(),g=f;break}}else d.cacheView&&(h.view[i]={});d.cacheView?(h.view[i][e]&&e!==g?h.view[i][e].html=null:h.view[i][e]||(h.view[i][e]={html:null,done:a.noop}),c=e):c=null}};this.options=e,this.appData={},this.cache=h,this.debug=j,this.deliver=function(b){return"function"==typeof b?e.apiCallback=b:"object"==typeof b?a.extend(e,b):"string"==typeof b&&(e.context=b),l()},this.merge=function(b){return a.extend(e,b),this},this.apiRequest=function(b){var i,j,k,l,m,n,o={href:b.attr("href"),url:b.attr(f+"url")||b.attr("action"),method:(b.attr(f+"method")||b.attr("method")||"get").toLowerCase(),cacheRequest:b.attr(f+"cache-request")||!1,cacheResponse:b.attr(f+"cache-response")||!1,cacheView:b.attr(f+"cache-view")||!1,focus:JSON.parse(b.attr(f+"focus")||!1),type:b.attr(f+"type")||"json",selector:b.attr(f+"selector")||!1,template:Handlebars.templates[b.attr(f+"template")]||!1,partial:Handlebars.partials[b.attr(f+"partial")]||!1,events:b.attr(f+"publish")?b.attr(f+"publish").split(" "):[],title:b.attr(f+"title")||b.attr("title")||b.text(),requestData:a.extend({},d.serializeObject(b),JSON.parse(b.attr(f+"form")||"{}")),templateData:{app:d.appData,view:b.data()}},q=function(a){var b=0;for(b;bLoading...",useHref:!1,xhrAlways:a.noop,xhrBeforeSend:a.noop,xhrDone:a.noop,xhrFail:a.noop},f="data-"+(e.dataNamespace?a.trim(e.dataNamespace)+"-":""),g={API_LINK:"a["+f+"selector],a["+f+"url],button["+f+"selector],button["+f+"url]",API_FORM:"form["+f+"selector],form["+f+"url]",AUTO_LOAD:"["+f+'auto-load="true"]',CLICK:"["+f+"click]",LOADING:"["+f+'role="loading"]'},h={response:{},view:{}},i=a({}),j=function(a){var b,c;if(e.debug&&"object"==typeof console&&("object"==typeof a||"function"==typeof console[a]))if("object"==typeof a)for(c in a)"function"==typeof console[c]?(b="string"==typeof a[c]||"object"==typeof a[c]&&void 0===a[c].length?[a[c]]:a[c],console[c].apply(console,b)):console.log(a[c]);else console[a].apply(console,Array.prototype.slice.call(arguments,1))},k=function(a){throw"Payload.js: "+a},l=function(){return b=a(e.context),b.length||k('Selector "'+e.context+'" not found'),m(),d},m=function(){n(),o()},n=function(){b.on("click.api-request auto-load.api-request",g.API_LINK,function(b){var c=a(this);b.preventDefault(),c.prop("disabled")||c.hasClass("disabled")||c.data("disabled")||e.apiOnClick(c,b)&&d.apiRequest(c)}).on("submit.api-request",g.API_FORM,function(b){var c=a(this);b.preventDefault(),e.apiOnSubmit(c,b)&&d.apiRequest(c)})},o=function(){b.on("click.click",g.CLICK,function(b){var c=a(this),d=a(c.attr(f+"click"));b.preventDefault(),d.click()})},p=function(b,d,e){var f,g,i=d.selector,j=a(i);if(!b.closest(i).length){if(h.view[i]){for(f in h.view[i])if(h.view[i].hasOwnProperty(f)&&null===h.view[i][f].html){h.view[i][f].html=j.contents().detach(),g=f;break}}else d.cacheView&&(h.view[i]={});d.cacheView?(h.view[i][e]&&e!==g?h.view[i][e].html=null:h.view[i][e]||(h.view[i][e]={html:null,done:a.noop}),c=e):c=null}};this.options=e,this.appData={},this.cache=h,this.debug=j,this.deliver=function(a){return"function"==typeof a?e.apiCallback=a:"object"==typeof a?d.merge(a):"string"==typeof a&&(e.context=a),l()},this.merge=function(b){return a.extend(e,b)},this.apiRequest=function(b){var i,j,k,l,m,n,o={href:b.attr("href"),url:b.attr(f+"url")||b.attr("action"),method:(b.attr(f+"method")||b.attr("method")||"get").toLowerCase(),cacheRequest:b.attr(f+"cache-request")||!1,cacheResponse:b.attr(f+"cache-response")||!1,cacheView:b.attr(f+"cache-view")||!1,focus:JSON.parse(b.attr(f+"focus")||!1),type:b.attr(f+"type")||"json",selector:b.attr(f+"selector")||!1,template:Handlebars.templates[b.attr(f+"template")]||!1,partial:Handlebars.partials[b.attr(f+"partial")]||!1,events:b.attr(f+"publish")?b.attr(f+"publish").split(" "):[],title:b.attr(f+"title")||b.attr("title")||b.text(),requestData:a.extend({},d.serializeObject(b),JSON.parse(b.attr(f+"form")||"{}")),templateData:{app:d.appData,view:b.data()}},q=function(a){var b=0;for(b;b