File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,12 +12,10 @@ import registry from "../../core/registry";
1212const log = logging . getLogger ( "pat.ajax" ) ;
1313
1414export const parser = new Parser ( "ajax" ) ;
15+ parser . addArgument ( "accept" , "text/html" ) ;
1516parser . addArgument ( "url" , function ( $el ) {
16- return ( $el . is ( "a" )
17- ? $el . attr ( "href" )
18- : $el . is ( "form" )
19- ? $el . attr ( "action" )
20- : ""
17+ return (
18+ $el . is ( "a" ) ? $el . attr ( "href" ) : $el . is ( "form" ) ? $el . attr ( "action" ) : ""
2119 ) . split ( "#" ) [ 0 ] ;
2220} ) ;
2321
@@ -104,10 +102,15 @@ var _ = {
104102 args = {
105103 context : $el ,
106104 data : [ $el . serialize ( ) , clickedData ] . filter ( Boolean ) . join ( "&" ) ,
105+ headers : { } ,
107106 url : cfg . url ,
108107 method : $el . attr ( "method" ) ? $el . attr ( "method" ) : "GET" ,
109108 } ;
110109
110+ if ( cfg . accept ) {
111+ args . headers . Accept = cfg . accept ;
112+ }
113+
111114 if (
112115 $el . is ( "form" ) &&
113116 $el . attr ( "method" ) &&
Original file line number Diff line number Diff line change @@ -135,5 +135,23 @@ describe("pat-ajax", function () {
135135 const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
136136 expect ( ajaxargs . url ) . toEqual ( "else.html" ) ;
137137 } ) ;
138+
139+ // Accept
140+ it ( "Default accept header" , function ( ) {
141+ document . body . innerHTML = `<a class="pat-ajax" />` ;
142+ registry . scan ( document . body ) ;
143+ jest . spyOn ( $ , "ajax" ) ;
144+ document . body . querySelector ( ".pat-ajax" ) . click ( ) ;
145+ const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
146+ expect ( ajaxargs . headers ) . toEqual ( { Accept : "text/html" } ) ;
147+ } ) ;
148+ it ( "Can set accept header" , function ( ) {
149+ document . body . innerHTML = `<a class="pat-ajax" data-pat-ajax="accept: */*"/>` ;
150+ registry . scan ( document . body ) ;
151+ jest . spyOn ( $ , "ajax" ) ;
152+ document . body . querySelector ( ".pat-ajax" ) . click ( ) ;
153+ const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
154+ expect ( ajaxargs . headers ) . toEqual ( { Accept : "*/*" } ) ;
155+ } ) ;
138156 } ) ;
139157} ) ;
You can’t perform that action at this time.
0 commit comments