Skip to content

Commit 299587e

Browse files
committed
Merge pull request #403 from defunkt/before-replace-event-tweaks
Before replace event tweaks
2 parents fc4b710 + 9baf0ac commit 299587e

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

jquery.pjax.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
404411
function 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 {

test/unit/pjax.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ if ($.support.pjax) {
547547
ok(contents)
548548
equal($(event.target).text(), beforeContent)
549549
equal(options.url, "hello.html")
550+
551+
ok(event.state.url.match("/hello.html"))
552+
ok(event.previousState.url.match("/home.html"))
553+
ok(frame.$.pjax.state.url.match("/hello.html"))
550554
})
551555
frame.$("#main").on("pjax:success", function(event) {
552556
notEqual($(event.target).text(), beforeContent)
@@ -864,8 +868,14 @@ if ($.support.pjax) {
864868
ok(event)
865869
ok(contents)
866870
equal(frame.location.pathname, "/home.html")
871+
ok(options.url.match("/home.html"))
867872
// Remember: the content hasn't yet been replaced.
868873
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+
869879
start()
870880
})
871881

0 commit comments

Comments
 (0)