@@ -159,6 +159,8 @@ function handleSubmit(event, container, options) {
159159//
160160// Returns whatever $.ajax returns.
161161function pjax ( options ) {
162+ var previousState = pjax . state ;
163+
162164 options = $ . extend ( true , { } , $ . ajaxSettings , pjax . defaults , options )
163165
164166 if ( $ . isFunction ( options . url ) ) {
@@ -178,8 +180,10 @@ function pjax(options) {
178180 if ( ! options . data ) options . data = { }
179181 options . data . _pjax = context . selector
180182
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 )
183187 context . trigger ( event , args )
184188 return ! event . isDefaultPrevented ( )
185189 }
@@ -253,8 +257,6 @@ function pjax(options) {
253257 return
254258 }
255259
256- var previousState = pjax . state ;
257-
258260 pjax . state = {
259261 id : options . id || uniqueId ( ) ,
260262 url : container . url ,
@@ -275,7 +277,10 @@ function pjax(options) {
275277
276278 if ( container . title ) document . title = container . title
277279
278- fire ( 'pjax:beforeReplace' , [ container . contents , options , previousState ] )
280+ fire ( 'pjax:beforeReplace' , [ container . contents , options ] , {
281+ state : pjax . state ,
282+ previousState : previousState
283+ } )
279284 context . html ( container . contents )
280285
281286 // FF bug: Won't autofocus fields that are inserted via JS.
@@ -404,6 +409,7 @@ if ('state' in window.history) {
404409// You probably shouldn't use pjax on pages with other pushState
405410// stuff yet.
406411function onPjaxPopstate ( event ) {
412+ var previousState = pjax . state ;
407413 var state = event . state
408414
409415 if ( state && state . container ) {
@@ -449,10 +455,13 @@ function onPjaxPopstate(event) {
449455 if ( contents ) {
450456 container . trigger ( 'pjax:start' , [ null , options ] )
451457
452- var previousState = pjax . state ;
453458 pjax . state = state
454459 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 ] )
456465 container . html ( contents )
457466
458467 container . trigger ( 'pjax:end' , [ null , options ] )
0 commit comments