From dbc48739e022bbb2e95b925ace2e0e47ec3d05b5 Mon Sep 17 00:00:00 2001 From: Dan Lasky Date: Fri, 18 Mar 2016 13:45:10 -0500 Subject: [PATCH] page retention on requests --- src/shared/js/ajaxpageplugin.html | 22 +++++++++++++++++++-- src/shared/js/request.html | 1 + test/lib_ajaxpageplugin.html | 32 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/shared/js/ajaxpageplugin.html b/src/shared/js/ajaxpageplugin.html index 47d52199..943524b0 100644 --- a/src/shared/js/ajaxpageplugin.html +++ b/src/shared/js/ajaxpageplugin.html @@ -24,7 +24,8 @@ query:true, body:false, pageType:"offset", - countPath:"" + countPath:"", + stampPage:true, },config); this.name = name; }; @@ -47,6 +48,10 @@ if (opts.enabled) { ajaxOpts = _getParamBase(ajaxOpts); var page = opts.pageType === "offset" ? opts.page * opts.pageSize : opts.page; + if (opts.stampPage) { + ajaxOpts._page = opts.page + ajaxOpts._pageSize = opts.pageSize; + } if (opts.url) { if (opts.pageName) { @@ -74,7 +79,20 @@ responseHandler:function(response) { if (this.config.enabled && this.config.countPath) { - response.count = DataUtils.getValuePath("response." + this.config.countPath, response); + response.count = DataUtils.getPathValue("response." + this.config.countPath, response); + } + + if (this.config.enabled && this.config.stampPage) { + var _page = DataUtils.getPathValue('instance.options._page', response); + var _pageSize = DataUtils.getPathValue('instance.options._pageSize', response); + var meta = DataUtils.getPathValue('marshalled.meta', response); + if (meta) { + meta._page = _page; + meta._pageSize = _pageSize; + } else { + response._page = _page; + response._pageSize = _pageSize; + } } return response; diff --git a/src/shared/js/request.html b/src/shared/js/request.html index 64af55dc..b26a9a5b 100644 --- a/src/shared/js/request.html +++ b/src/shared/js/request.html @@ -31,6 +31,7 @@ this.progress = options.progress; this.requestMarshaller = options.requestMarshaller; this.responseMarshaller = options.responseMarshaller; + this.options = options; this.defer = {}; this.xhr = new XMLHttpRequest(); diff --git a/test/lib_ajaxpageplugin.html b/test/lib_ajaxpageplugin.html index 007a3fac..6f65e7e3 100644 --- a/test/lib_ajaxpageplugin.html +++ b/test/lib_ajaxpageplugin.html @@ -130,6 +130,38 @@ result2.query[0].value.should.equal(2); }); + it("should be able to stamp the page data into the response", function() { + var pg1 = new StrandLib.AjaxPagePlugin({ + enabled:true, + pageType:"index", + page:2, + stampPage:true + }); + var result0 = pg1.requestHandler({body:{}}); + result0.query[0].value.should.equal(2); + var result1 = pg1.responseHandler({ + result:{}, + instance:{ + options:{ + _page:1, + _pageSize:1} + }, + marshalled:{meta:{}}}); + result1.marshalled.meta._page.should.equal(1); + result1.marshalled.meta._pageSize.should.equal(1); + var result2 = pg1.responseHandler({ + result:{}, + instance:{ + options:{ + _page:1, + _pageSize:1} + }, + }); + result2._page.should.equal(1); + result2._pageSize.should.equal(1); + + }); + it("should stop when disabled", function() { //disabled is default for paging since only collections need it var pg = new StrandLib.AjaxPagePlugin();