@@ -178,8 +178,10 @@ function pjax(options) {
178
178
if ( ! options . data ) options . data = { }
179
179
options . data . _pjax = context . selector
180
180
181
- function fire ( type , args ) {
182
- var event = $ . Event ( type , { relatedTarget : target } )
181
+ function fire ( type , args , props ) {
182
+ if ( ! props ) props = { }
183
+ props . relatedTarget = target
184
+ var event = $ . Event ( type , props )
183
185
context . trigger ( event , args )
184
186
return ! event . isDefaultPrevented ( )
185
187
}
@@ -231,6 +233,8 @@ function pjax(options) {
231
233
}
232
234
233
235
options . success = function ( data , status , xhr ) {
236
+ var previousState = pjax . state ;
237
+
234
238
// If $.pjax.defaults.version is a function, invoke it first.
235
239
// Otherwise it can be a static string.
236
240
var currentVersion = ( typeof $ . pjax . defaults . version === 'function' ) ?
@@ -273,7 +277,10 @@ function pjax(options) {
273
277
274
278
if ( container . title ) document . title = container . title
275
279
276
- fire ( 'pjax:beforeReplace' , [ container . contents , options ] )
280
+ fire ( 'pjax:beforeReplace' , [ container . contents , options ] , {
281
+ state : pjax . state ,
282
+ previousState : previousState
283
+ } )
277
284
context . html ( container . contents )
278
285
279
286
// FF bug: Won't autofocus fields that are inserted via JS.
@@ -402,6 +409,7 @@ if ('state' in window.history) {
402
409
// You probably shouldn't use pjax on pages with other pushState
403
410
// stuff yet.
404
411
function onPjaxPopstate ( event ) {
412
+ var previousState = pjax . state ;
405
413
var state = event . state
406
414
407
415
if ( state && state . container ) {
@@ -447,10 +455,14 @@ function onPjaxPopstate(event) {
447
455
if ( contents ) {
448
456
container . trigger ( 'pjax:start' , [ null , options ] )
449
457
458
+ pjax . state = state
450
459
if ( state . title ) document . title = state . title
451
- container . trigger ( 'pjax:beforeReplace' , [ contents , options ] )
460
+ var beforeReplaceEvent = $ . Event ( 'pjax:beforeReplace' , {
461
+ state : state ,
462
+ previousState : previousState
463
+ } )
464
+ container . trigger ( beforeReplaceEvent , [ contents , options ] )
452
465
container . html ( contents )
453
- pjax . state = state
454
466
455
467
container . trigger ( 'pjax:end' , [ null , options ] )
456
468
} else {
0 commit comments