Skip to content

Commit d08a2f7

Browse files
Henric Trotzig + Joe Lencionimislav
authored andcommitted
Fire pjax:beforeReplace event before replacing HTML
This event fires right before DOM replacement and it's useful if any processing of old content is required, such as unmounting components. This is similar to the equivalent event that Turbolinks [1] fires. Fixes #383 [1]: https://github.com/rails/turbolinks
1 parent d899eaa commit d08a2f7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

jquery.pjax.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ function pjax(options) {
272272
} catch (e) { }
273273

274274
if (container.title) document.title = container.title
275+
276+
fire('pjax:beforeReplace', [container.contents, options])
275277
context.html(container.contents)
276278

277279
// FF bug: Won't autofocus fields that are inserted via JS.
@@ -446,6 +448,7 @@ function onPjaxPopstate(event) {
446448
container.trigger('pjax:start', [null, options])
447449

448450
if (state.title) document.title = state.title
451+
container.trigger('pjax:beforeReplace', [contents, options])
449452
container.html(contents)
450453
pjax.state = state
451454

test/unit/pjax.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,28 @@ if ($.support.pjax) {
536536
})
537537
})
538538

539+
asyncTest("triggers pjax:beforeReplace event from container", function() {
540+
var frame = this.frame,
541+
beforeContent = 'foo'
542+
543+
frame.$("#main")
544+
.text(beforeContent)
545+
.on("pjax:beforeReplace", function(event, contents, options) {
546+
ok(event)
547+
ok(contents)
548+
equal($(event.target).text(), beforeContent)
549+
equal(options.url, "hello.html")
550+
})
551+
frame.$("#main").on("pjax:success", function(event) {
552+
notEqual($(event.target).text(), beforeContent)
553+
start()
554+
})
555+
556+
frame.$.pjax({
557+
url: "hello.html",
558+
container: "#main"
559+
})
560+
})
539561

540562
asyncTest("triggers pjax:success event from container", function() {
541563
var frame = this.frame
@@ -828,6 +850,34 @@ if ($.support.pjax) {
828850
})
829851
})
830852

853+
asyncTest("popstate triggers pjax:beforeReplace event", function() {
854+
var frame = this.frame,
855+
originalContent = $(frame).html()
856+
857+
equal(frame.location.pathname, "/home.html")
858+
859+
frame.$('#main').on("pjax:complete", function() {
860+
equal(frame.location.pathname, "/hello.html")
861+
ok(frame.history.length > 1)
862+
863+
frame.$('#main').on('pjax:beforeReplace', function(event, contents, options) {
864+
ok(event)
865+
ok(contents)
866+
equal(frame.location.pathname, "/home.html")
867+
// Remember: the content hasn't yet been replaced.
868+
notEqual($(event.target).html(), originalContent)
869+
start()
870+
})
871+
872+
goBack(frame, function() {})
873+
})
874+
875+
frame.$.pjax({
876+
url: "hello.html",
877+
container: "#main"
878+
})
879+
})
880+
831881
// Test is fragile
832882
asyncTest("no initial pjax:popstate event", function() {
833883
var frame = this.frame

0 commit comments

Comments
 (0)