19
19
// pjax specific options:
20
20
//
21
21
//
22
- // container - Where to stick the response body. Usually a String selector.
23
- // $(container).html(xhr.responseBody)
24
- // (default: current jquery context)
22
+ // container - String selector for the element where to place the response body.
25
23
// push - Whether to pushState the URL. Defaults to true (of course).
26
24
// replace - Want to use replaceState instead? That's cool.
27
25
//
30
28
//
31
29
// Returns the jQuery object
32
30
function fnPjax ( selector , container , options ) {
33
- var context = this
31
+ options = optionsFor ( container , options )
34
32
return this . on ( 'click.pjax' , selector , function ( event ) {
35
- var opts = $ . extend ( { } , optionsFor ( container , options ) )
36
- if ( ! opts . container )
37
- opts . container = $ ( this ) . attr ( 'data-pjax' ) || context
33
+ var opts = options
34
+ if ( ! opts . container ) {
35
+ opts = $ . extend ( { } , options )
36
+ opts . container = $ ( this ) . attr ( 'data-pjax' )
37
+ }
38
38
handleClick ( event , opts )
39
39
} )
40
40
}
@@ -52,11 +52,6 @@ function fnPjax(selector, container, options) {
52
52
// // is the same as
53
53
// $(document).pjax('a')
54
54
//
55
- // $(document).on('click', 'a', function(event) {
56
- // var container = $(this).closest('[data-pjax-container]')
57
- // $.pjax.click(event, container)
58
- // })
59
- //
60
55
// Returns nothing.
61
56
function handleClick ( event , container , options ) {
62
57
options = optionsFor ( container , options )
@@ -110,8 +105,7 @@ function handleClick(event, container, options) {
110
105
// Examples
111
106
//
112
107
// $(document).on('submit', 'form', function(event) {
113
- // var container = $(this).closest('[data-pjax-container]')
114
- // $.pjax.submit(event, container)
108
+ // $.pjax.submit(event, '[data-pjax-container]')
115
109
// })
116
110
//
117
111
// Returns nothing.
@@ -158,8 +152,7 @@ function handleSubmit(event, container, options) {
158
152
//
159
153
// Accepts these extra keys:
160
154
//
161
- // container - Where to stick the response body.
162
- // $(container).html(xhr.responseBody)
155
+ // container - String selector for where to stick the response body.
163
156
// push - Whether to pushState the URL. Defaults to true (of course).
164
157
// replace - Want to use replaceState instead? That's cool.
165
158
//
@@ -176,26 +169,31 @@ function pjax(options) {
176
169
options . url = options . url ( )
177
170
}
178
171
179
- var target = options . target
180
-
181
172
var hash = parseURL ( options . url ) . hash
182
173
183
- var context = options . context = findContainerFor ( options . container )
174
+ var containerType = $ . type ( options . container )
175
+ if ( containerType !== 'string' ) {
176
+ throw "expected string value for 'container' option; got " + containerType
177
+ }
178
+ var context = options . context = $ ( options . container )
179
+ if ( ! context . length ) {
180
+ throw "the container selector '" + options . container + "' did not match anything"
181
+ }
184
182
185
183
// We want the browser to maintain two separate internal caches: one
186
184
// for pjax'd partial page loads and one for normal page loads.
187
185
// Without adding this secret parameter, some browsers will often
188
186
// confuse the two.
189
187
if ( ! options . data ) options . data = { }
190
188
if ( $ . isArray ( options . data ) ) {
191
- options . data . push ( { name : '_pjax' , value : context . selector } )
189
+ options . data . push ( { name : '_pjax' , value : options . container } )
192
190
} else {
193
- options . data . _pjax = context . selector
191
+ options . data . _pjax = options . container
194
192
}
195
193
196
194
function fire ( type , args , props ) {
197
195
if ( ! props ) props = { }
198
- props . relatedTarget = target
196
+ props . relatedTarget = options . target
199
197
var event = $ . Event ( type , props )
200
198
context . trigger ( event , args )
201
199
return ! event . isDefaultPrevented ( )
@@ -211,7 +209,7 @@ function pjax(options) {
211
209
}
212
210
213
211
xhr . setRequestHeader ( 'X-PJAX' , 'true' )
214
- xhr . setRequestHeader ( 'X-PJAX-Container' , context . selector )
212
+ xhr . setRequestHeader ( 'X-PJAX-Container' , options . container )
215
213
216
214
if ( ! fire ( 'pjax:beforeSend' , [ xhr , settings ] ) )
217
215
return false
@@ -284,7 +282,7 @@ function pjax(options) {
284
282
id : options . id || uniqueId ( ) ,
285
283
url : container . url ,
286
284
title : container . title ,
287
- container : context . selector ,
285
+ container : options . container ,
288
286
fragment : options . fragment ,
289
287
timeout : options . timeout
290
288
}
@@ -347,7 +345,7 @@ function pjax(options) {
347
345
id : uniqueId ( ) ,
348
346
url : window . location . href ,
349
347
title : document . title ,
350
- container : context . selector ,
348
+ container : options . container ,
351
349
fragment : options . fragment ,
352
350
timeout : options . timeout
353
351
}
@@ -363,7 +361,7 @@ function pjax(options) {
363
361
if ( xhr . readyState > 0 ) {
364
362
if ( options . push && ! options . replace ) {
365
363
// Cache current container element before replacing it
366
- cachePush ( pjax . state . id , cloneContents ( context ) )
364
+ cachePush ( pjax . state . id , [ options . container , cloneContents ( context ) ] )
367
365
368
366
window . history . pushState ( null , "" , options . requestUrl )
369
367
}
@@ -448,13 +446,14 @@ function onPjaxPopstate(event) {
448
446
}
449
447
450
448
var cache = cacheMapping [ state . id ] || [ ]
451
- var container = $ ( cache [ 0 ] || state . container ) , contents = cache [ 1 ]
449
+ var containerSelector = cache [ 0 ] || state . container
450
+ var container = $ ( containerSelector ) , contents = cache [ 1 ]
452
451
453
452
if ( container . length ) {
454
453
if ( previousState ) {
455
454
// Cache current container before replacement and inform the
456
455
// cache which direction the history shifted.
457
- cachePop ( direction , previousState . id , cloneContents ( container ) )
456
+ cachePop ( direction , previousState . id , [ containerSelector , cloneContents ( container ) ] )
458
457
}
459
458
460
459
var popstateEvent = $ . Event ( 'pjax:popstate' , {
@@ -466,7 +465,7 @@ function onPjaxPopstate(event) {
466
465
var options = {
467
466
id : state . id ,
468
467
url : state . url ,
469
- container : container ,
468
+ container : containerSelector ,
470
469
push : false ,
471
470
fragment : state . fragment ,
472
471
timeout : state . timeout ,
@@ -568,7 +567,7 @@ function cloneContents(container) {
568
567
cloned . find ( 'script' ) . each ( function ( ) {
569
568
if ( ! this . src ) jQuery . _data ( this , 'globalEval' , false )
570
569
} )
571
- return [ container . selector , cloned . contents ( ) ]
570
+ return cloned . contents ( )
572
571
}
573
572
574
573
// Internal: Strip internal query params from parsed URL.
@@ -618,49 +617,14 @@ function stripHash(location) {
618
617
//
619
618
// Returns options Object.
620
619
function optionsFor ( container , options ) {
621
- // Both container and options
622
- if ( container && options )
620
+ if ( container && options ) {
621
+ options = $ . extend ( { } , options )
623
622
options . container = container
624
-
625
- // First argument is options Object
626
- else if ( $ . isPlainObject ( container ) )
627
- options = container
628
-
629
- // Only container
630
- else
631
- options = { container : container }
632
-
633
- // Find and validate container
634
- if ( options . container )
635
- options . container = findContainerFor ( options . container )
636
-
637
- return options
638
- }
639
-
640
- // Internal: Find container element for a variety of inputs.
641
- //
642
- // Because we can't persist elements using the history API, we must be
643
- // able to find a String selector that will consistently find the Element.
644
- //
645
- // container - A selector String or jQuery object.
646
- //
647
- // Returns a jQuery object whose context is `document` and has a selector.
648
- function findContainerFor ( container ) {
649
- var formatedContainer
650
-
651
- if ( jQuery . type ( container ) === 'string' ) {
652
- formatedContainer = $ ( container )
653
- formatedContainer . selector = container
654
- } else {
655
- formatedContainer = container
656
- }
657
-
658
- if ( ! formatedContainer . length ) {
659
- throw "no pjax container for " + container
660
- } else if ( ! formatedContainer . selector ) {
661
- throw "cant get selector for pjax container"
623
+ return options
624
+ } else if ( $ . isPlainObject ( container ) ) {
625
+ return container
662
626
} else {
663
- return formatedContainer
627
+ return { container : container }
664
628
}
665
629
}
666
630
0 commit comments