@@ -159,6 +159,8 @@ function handleSubmit(event, container, options) {
159
159
//
160
160
// Returns whatever $.ajax returns.
161
161
function pjax ( options ) {
162
+ var previousState = pjax . state ;
163
+
162
164
options = $ . extend ( true , { } , $ . ajaxSettings , pjax . defaults , options )
163
165
164
166
if ( $ . isFunction ( options . url ) ) {
@@ -178,8 +180,10 @@ function pjax(options) {
178
180
if ( ! options . data ) options . data = { }
179
181
options . data . _pjax = context . selector
180
182
181
- function fire ( type , args ) {
182
- var event = $ . Event ( type , { relatedTarget : target } )
183
+ function fire ( type , args , props ) {
184
+ if ( ! props ) props = { }
185
+ props . relatedTarget = target
186
+ var event = $ . Event ( type , props )
183
187
context . trigger ( event , args )
184
188
return ! event . isDefaultPrevented ( )
185
189
}
@@ -253,8 +257,6 @@ function pjax(options) {
253
257
return
254
258
}
255
259
256
- var previousState = pjax . state ;
257
-
258
260
pjax . state = {
259
261
id : options . id || uniqueId ( ) ,
260
262
url : container . url ,
@@ -275,7 +277,10 @@ function pjax(options) {
275
277
276
278
if ( container . title ) document . title = container . title
277
279
278
- fire ( 'pjax:beforeReplace' , [ container . contents , options , previousState ] )
280
+ fire ( 'pjax:beforeReplace' , [ container . contents , options ] , {
281
+ state : pjax . state ,
282
+ previousState : previousState
283
+ } )
279
284
context . html ( container . contents )
280
285
281
286
// FF bug: Won't autofocus fields that are inserted via JS.
@@ -404,6 +409,7 @@ if ('state' in window.history) {
404
409
// You probably shouldn't use pjax on pages with other pushState
405
410
// stuff yet.
406
411
function onPjaxPopstate ( event ) {
412
+ var previousState = pjax . state ;
407
413
var state = event . state
408
414
409
415
if ( state && state . container ) {
@@ -449,10 +455,13 @@ function onPjaxPopstate(event) {
449
455
if ( contents ) {
450
456
container . trigger ( 'pjax:start' , [ null , options ] )
451
457
452
- var previousState = pjax . state ;
453
458
pjax . state = state
454
459
if ( state . title ) document . title = state . title
455
- container . trigger ( 'pjax:beforeReplace' , [ contents , options , previousState ] )
460
+ var beforeReplaceEvent = $ . Event ( 'pjax:beforeReplace' , {
461
+ state : state ,
462
+ previousState : previousState
463
+ } )
464
+ container . trigger ( beforeReplaceEvent , [ contents , options ] )
456
465
container . html ( contents )
457
466
458
467
container . trigger ( 'pjax:end' , [ null , options ] )
0 commit comments