Skip to content

Commit 4c13d0e

Browse files
committed
Fixes and improvements
1 parent 4911f9d commit 4c13d0e

File tree

7 files changed

+658
-3062
lines changed

7 files changed

+658
-3062
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,8 @@ QQuery allows you to create your own extension. Let us show you how! First of al
494494
To register an extension, you do the following:
495495
```js
496496
QQuery.extensions.yourExtension = (q, parameters) => {
497-
var selector = q;
498497
//q = The selector object
499-
//You can use other QQuery extensions/functions on h if you want, like .css and .html.
498+
//You can use other QQuery extensions/functions on q if you want, like .css and .html.
500499

501500
if(parameters == 'Yes!') {
502501
q.css('color', 'green').html('Yes!');
@@ -506,15 +505,15 @@ QQuery.extensions.yourExtension = (q, parameters) => {
506505
return q;
507506
};
508507
```
509-
On the place of "parameters" you can add as much as parameters as you want. The q parameter is the selector, make sure you include it even when you are'nt using it.
508+
On the place of "parameters" you can add as much as parameters as you want. The q parameter is the selector, make sure you include it even when you aren't using it.
510509

511510
You can call your extension like this:
512511
```js
513512
$('#myelement').yourExtension(parameters);
514513
//Notice that q isn't available, q is passed by QQuery. It is just the $('#myelement') selector object.
515514
```
516515

517-
If you are using NodeJS, you have to use the global like this:
516+
If you are using NodeJS, you have to use the global extension setup like this:
518517
```global.QQuery.extensions.myExtension = (q, ...)```
519518

520519
## Helpers
@@ -580,7 +579,7 @@ $.get('/getdata', (r, status) => {
580579
```
581580

582581
### $.post(url, data, callback, resType = 'json', reqType = 'urlencoded')
583-
Post data to the server. This is also a hlper for $.ajax
582+
Post data to the server. This is also a helper for $.ajax
584583
```js
585584
$.post('/postdata', {name: 'henk'}, (r, code) => {
586585
if(code === 200) {
@@ -650,7 +649,12 @@ setup: {
650649
//The value function returns the XSRF token from the page
651650
value: () => $('meta[name="csrf-token"]').attr('content'),
652651
//The hasValue method indicates if the token is present.
653-
hasValue: () => $('meta[name="csrf-token"]').any()
652+
hasValue: () => $('meta[name="csrf-token"]').any(),
653+
654+
//The before callback is called before any AJAX request is sent
655+
before: () => {},
656+
//The after callback is called after any AJAX response is received. You can use this for a global error handler or sth like that
657+
after: (statusCode) => {}
654658
}
655659
},
656660
```
@@ -659,6 +663,15 @@ setup: {
659663

660664
If you are using nodeJS, you have to use ```global.QQuery.setup``` instead of ```QQuery.setup``` to access the settings.
661665

666+
In a React application, you can do this using an useEffect hook.
667+
```js
668+
//QQuery setup
669+
useEffect(() => {
670+
global.QQuery.setup.ajax.value = () => $.cookie('XSRF-TOKEN');
671+
global.QQuery.setup.ajax.hasValue = () => !!$.cookie('XSRF-TOKEN');
672+
}, []);
673+
```
674+
662675
By default, QQuery uses a X-CSRF-TOKEN header in every request. The token is obtained from the csrf-token meta-tag. This looks like this:
663676
```html
664677
<meta name="csrf-token" content="YOUR_CSRF_TOKEN">
@@ -677,6 +690,7 @@ C# startup.cs
677690
```cs
678691
//In ConfigureServices
679692
services.AddAntiforgery(options => options.HeaderName = "X-CSRF-TOKEN");
693+
680694
//Configure
681695
public void Configure(IApplicationBuilder app, IAntiforgery antiforgery)
682696
{

0 commit comments

Comments
 (0)