Skip to content

Commit fd427e3

Browse files
committed
Add data param to .load() method in navigation for one-time data
1 parent 9739aa2 commit fd427e3

File tree

9 files changed

+44
-28
lines changed

9 files changed

+44
-28
lines changed

README-template.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ setIncomingElement|el:string|self|a selector string for the element being retrie
122122
getIncomingElement| |string|
123123
setReplaceElement|el:string|self|a selector string for the element on the current page you want the new HTML to replace
124124
getReplaceElement| |string|
125-
load|url:string, callback:function/null, incoming_el:string/null, replace_el:string/null, push_state:bool|void|pulls content from the provided URL and puts it on the current page - also swaps out the page title, metas, and much more
125+
load|url:string, callback:function/null, data:object, incoming_el:string/null, replace_el:string/null, push_state:bool|void|pulls content from the provided URL and puts it on the current page - also swaps out the page title, metas, and much more
126126
loaderEnabled|n/a|bool|property to toggle the slow request loader on/off
127127
setLoaderDelay|delay:int|self|set how long a request should take in ms before the loader displays
128128
getLoaderDelay| |self|
@@ -241,18 +241,23 @@ navigation.setDataItem('product_id', 1);
241241
navigation.load('/my-page', function(el, el_selector, replaced_selector, route, data){
242242
//my page is now loaded
243243
244-
//get the product_id that was set
244+
//get the product_id that was set using .setDataItem() above
245245
const product_id = data.product_id;
246246
247-
//now clear it so it's gone for the next page load
247+
//get some_other_id that was provided as the third .load param (right below this callback)
248+
//some_other_id is only for this onload callback and does not persist
249+
//the product_id value above will persist unless cleared
250+
const some_other_id = data.some_other_id;
251+
252+
//now clear product_id it so it's gone for the next page load
248253
navigation.clearData();
249-
});
254+
}, {some_other_id: 2});
250255
251256
//in some cases, you'll want to grab a different element from the URL
252257
//this example grabs .popup-content from /my-popup and replaces .current-popup
253258
navigation.load('/my-popup', function(el, el_selector, replaced_selector, route, data){
254259
//now the new element is on the page
255-
}, '.popup-content', '.current-popup');
260+
}, {}, '.popup-content', '.current-popup');
256261
```
257262
258263
---

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ setIncomingElement|el:string|self|a selector string for the element being retrie
122122
getIncomingElement| |string|
123123
setReplaceElement|el:string|self|a selector string for the element on the current page you want the new HTML to replace
124124
getReplaceElement| |string|
125-
load|url:string, callback:function/null, incoming_el:string/null, replace_el:string/null, push_state:bool|void|pulls content from the provided URL and puts it on the current page - also swaps out the page title, metas, and much more
125+
load|url:string, callback:function/null, data:object, incoming_el:string/null, replace_el:string/null, push_state:bool|void|pulls content from the provided URL and puts it on the current page - also swaps out the page title, metas, and much more
126126
loaderEnabled|n/a|bool|property to toggle the slow request loader on/off
127127
setLoaderDelay|delay:int|self|set how long a request should take in ms before the loader displays
128128
getLoaderDelay| |self|
@@ -241,18 +241,23 @@ navigation.setDataItem('product_id', 1);
241241
navigation.load('/my-page', function(el, el_selector, replaced_selector, route, data){
242242
//my page is now loaded
243243
244-
//get the product_id that was set
244+
//get the product_id that was set using .setDataItem() above
245245
const product_id = data.product_id;
246246
247-
//now clear it so it's gone for the next page load
247+
//get some_other_id that was provided as the third .load param (right below this callback)
248+
//some_other_id is only for this onload callback and does not persist
249+
//the product_id value above will persist unless cleared
250+
const some_other_id = data.some_other_id;
251+
252+
//now clear product_id it so it's gone for the next page load
248253
navigation.clearData();
249-
});
254+
}, {some_other_id: 2});
250255
251256
//in some cases, you'll want to grab a different element from the URL
252257
//this example grabs .popup-content from /my-popup and replaces .current-popup
253258
navigation.load('/my-popup', function(el, el_selector, replaced_selector, route, data){
254259
//now the new element is on the page
255-
}, '.popup-content', '.current-popup');
260+
}, {}, '.popup-content', '.current-popup');
256261
```
257262
258263
---

dist/jpack.bundled.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jpack.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

es/navigation/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,16 @@ export const navigation = {
151151
* 3) Replaces it on the page (and all the magic replacePageContent does, see comments on that method below)
152152
* 4) If there's a callback provided, it'll be run afterwards (it receives the newly replaced element as a param)
153153
*
154-
* On error, it triggers a navigation failure and provides the error message
154+
* On error, it triggers onFail callbacks you've attached and provides the error message
155155
*
156156
* @param url
157157
* @param callback
158+
* @param data
158159
* @param incoming_el
159160
* @param replace_el
160161
* @param push_state
161162
*/
162-
load: function (url, callback, incoming_el, replace_el, push_state = true) {
163+
load: function (url, callback, data = {}, incoming_el, replace_el, push_state = true) {
163164
const self = this;
164165

165166
//defaults
@@ -171,8 +172,10 @@ export const navigation = {
171172
if (typeof incoming_el !== 'string') throw `incoming_el (${incoming_el}) must be a string`;
172173
if (typeof replace_el !== 'string') throw `replace_el (${replace_el}) must be a string`;
173174

174-
//cache in case it changes during this process because axios is async
175-
const data = self.getData();
175+
//merge data set on navigation with data for this request
176+
data = {...self._data, ...data};
177+
178+
//cache route (axios is async)
176179
const current_route = self.getRouteFromMeta();
177180

178181
self.showLoader();
@@ -391,7 +394,7 @@ export const navigation = {
391394

392395
//back button
393396
window.onpopstate = function (e) {
394-
self.load(request.getURIWithQueryString(), null, self.getIncomingElement(), self.getReplaceElement(), false);
397+
self.load(request.getURIWithQueryString(), null, {}, self.getIncomingElement(), self.getReplaceElement(), false);
395398
};
396399

397400
return this;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@htmlguyllc/jpack",
3-
"version": "9.0.12",
3+
"version": "9.0.13",
44
"description": "Core Javascript Library of Everyday Objects, Events, and Utilities",
55
"keywords": [
66
"javascript",

test/_jpack.bundled.js

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/es-clone.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('clone', function() {
8383
obj.name = 'tom';
8484
if( obj.name === objClone.name ) throw 'Obj was modified: '+JSON.stringify(obj);
8585
obj = 'string now';
86-
if( objClone === 'string now' ) throw 'Obj was modified: '+JSON.stringify(obj);
86+
if( objClone === obj ) throw 'Obj was modified: '+JSON.stringify(obj);
8787

8888
//array
8989
var arr = [1,2];
@@ -96,7 +96,7 @@ describe('clone', function() {
9696
arr.push(3);
9797
if( arrClone[2] === 3 ) throw 'Array was modified: '+JSON.stringify(arrClone);
9898
arr = 'string now';
99-
if( typeof arrClone === 'string' ) throw `Array was modified: ${arrClone}`;
99+
if( arrClone === arr ) throw `Array was modified: ${arrClone}`;
100100
});
101101
});
102102
});

0 commit comments

Comments
 (0)