@@ -178,8 +178,10 @@ function pjax(options) {
178178 if ( ! options . data ) options . data = { }
179179 options . data . _pjax = context . selector
180180
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 )
183185 context . trigger ( event , args )
184186 return ! event . isDefaultPrevented ( )
185187 }
@@ -231,6 +233,8 @@ function pjax(options) {
231233 }
232234
233235 options . success = function ( data , status , xhr ) {
236+ var previousState = pjax . state ;
237+
234238 // If $.pjax.defaults.version is a function, invoke it first.
235239 // Otherwise it can be a static string.
236240 var currentVersion = ( typeof $ . pjax . defaults . version === 'function' ) ?
@@ -273,7 +277,10 @@ function pjax(options) {
273277
274278 if ( container . title ) document . title = container . title
275279
276- fire ( 'pjax:beforeReplace' , [ container . contents , options ] )
280+ fire ( 'pjax:beforeReplace' , [ container . contents , options ] , {
281+ state : pjax . state ,
282+ previousState : previousState
283+ } )
277284 context . html ( container . contents )
278285
279286 // FF bug: Won't autofocus fields that are inserted via JS.
@@ -402,6 +409,7 @@ if ('state' in window.history) {
402409// You probably shouldn't use pjax on pages with other pushState
403410// stuff yet.
404411function onPjaxPopstate ( event ) {
412+ var previousState = pjax . state ;
405413 var state = event . state
406414
407415 if ( state && state . container ) {
@@ -447,10 +455,14 @@ function onPjaxPopstate(event) {
447455 if ( contents ) {
448456 container . trigger ( 'pjax:start' , [ null , options ] )
449457
458+ pjax . state = state
450459 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 ] )
452465 container . html ( contents )
453- pjax . state = state
454466
455467 container . trigger ( 'pjax:end' , [ null , options ] )
456468 } else {
0 commit comments