Skip to content

Commit 5e563fb

Browse files
committed
Move state and previousState to event props
1 parent 85656fd commit 5e563fb

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

jquery.pjax.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ function handleSubmit(event, container, options) {
159159
//
160160
// Returns whatever $.ajax returns.
161161
function 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.
406411
function 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])

test/unit/pjax.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,14 @@ if ($.support.pjax) {
542542

543543
frame.$("#main")
544544
.text(beforeContent)
545-
.on("pjax:beforeReplace", function(event, contents, options, previousState) {
545+
.on("pjax:beforeReplace", function(event, contents, options) {
546546
ok(event)
547547
ok(contents)
548548
equal($(event.target).text(), beforeContent)
549549
equal(options.url, "hello.html")
550-
ok(previousState.url.match("/home.html"))
550+
551+
ok(event.state.url.match("/hello.html"))
552+
ok(event.previousState.url.match("/home.html"))
551553
ok(frame.$.pjax.state.url.match("/hello.html"))
552554
})
553555
frame.$("#main").on("pjax:success", function(event) {
@@ -862,15 +864,18 @@ if ($.support.pjax) {
862864
equal(frame.location.pathname, "/hello.html")
863865
ok(frame.history.length > 1)
864866

865-
frame.$('#main').on('pjax:beforeReplace', function(event, contents, options, previousState) {
867+
frame.$('#main').on('pjax:beforeReplace', function(event, contents, options) {
866868
ok(event)
867869
ok(contents)
868870
equal(frame.location.pathname, "/home.html")
869871
ok(options.url.match("/home.html"))
870-
ok(previousState.url.match("/hello.html"))
871-
ok(frame.$.pjax.state.url.match("/home.html"))
872872
// Remember: the content hasn't yet been replaced.
873873
notEqual($(event.target).html(), originalContent)
874+
875+
ok(event.state.url.match("/home.html"))
876+
ok(event.previousState.url.match("/hello.html"))
877+
ok(frame.$.pjax.state.url.match("/home.html"))
878+
874879
start()
875880
})
876881

0 commit comments

Comments
 (0)