Skip to content

Commit 85656fd

Browse files
committed
Add previous state to beforeReplace event
1 parent ee0887c commit 85656fd

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

jquery.pjax.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ function pjax(options) {
253253
return
254254
}
255255

256+
var previousState = pjax.state;
257+
256258
pjax.state = {
257259
id: options.id || uniqueId(),
258260
url: container.url,
@@ -273,7 +275,7 @@ function pjax(options) {
273275

274276
if (container.title) document.title = container.title
275277

276-
fire('pjax:beforeReplace', [container.contents, options])
278+
fire('pjax:beforeReplace', [container.contents, options, previousState])
277279
context.html(container.contents)
278280

279281
// FF bug: Won't autofocus fields that are inserted via JS.
@@ -447,9 +449,10 @@ function onPjaxPopstate(event) {
447449
if (contents) {
448450
container.trigger('pjax:start', [null, options])
449451

452+
var previousState = pjax.state;
450453
pjax.state = state
451454
if (state.title) document.title = state.title
452-
container.trigger('pjax:beforeReplace', [contents, options])
455+
container.trigger('pjax:beforeReplace', [contents, options, previousState])
453456
container.html(contents)
454457

455458
container.trigger('pjax:end', [null, options])

test/unit/pjax.js

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

543543
frame.$("#main")
544544
.text(beforeContent)
545-
.on("pjax:beforeReplace", function(event, contents, options) {
545+
.on("pjax:beforeReplace", function(event, contents, options, previousState) {
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"))
550551
ok(frame.$.pjax.state.url.match("/hello.html"))
551552
})
552553
frame.$("#main").on("pjax:success", function(event) {
@@ -861,13 +862,15 @@ if ($.support.pjax) {
861862
equal(frame.location.pathname, "/hello.html")
862863
ok(frame.history.length > 1)
863864

864-
frame.$('#main').on('pjax:beforeReplace', function(event, contents, options) {
865+
frame.$('#main').on('pjax:beforeReplace', function(event, contents, options, previousState) {
865866
ok(event)
866867
ok(contents)
867868
equal(frame.location.pathname, "/home.html")
869+
ok(options.url.match("/home.html"))
870+
ok(previousState.url.match("/hello.html"))
871+
ok(frame.$.pjax.state.url.match("/home.html"))
868872
// Remember: the content hasn't yet been replaced.
869873
notEqual($(event.target).html(), originalContent)
870-
ok(frame.$.pjax.state.url.match("/home.html"))
871874
start()
872875
})
873876

0 commit comments

Comments
 (0)