Skip to content

Commit a200310

Browse files
committed
Merge branch 'before-replace'
Fixes #383, closes #384
2 parents d899eaa + 23249a6 commit a200310

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ container, not the link that was clicked.
230230
<td><code>options</code></td>
231231
<td>fires after pjax has started from a link that got clicked</td>
232232
</tr>
233+
<tr>
234+
<td><code>pjax:beforeReplace</code></td>
235+
<td></td>
236+
<td><code>contents, options</code></td>
237+
<td>before replacing HTML with content loaded from the server</td>
238+
</tr>
233239
<tr>
234240
<td><code>pjax:success</code></td>
235241
<td></td>
@@ -275,6 +281,12 @@ container, not the link that was clicked.
275281
<td><code>null, options</code></td>
276282
<td>before replacing content</td>
277283
</tr>
284+
<tr>
285+
<td><code>pjax:beforeReplace</code></td>
286+
<td></td>
287+
<td><code>contents, options</code></td>
288+
<td>right before replacing HTML with content from cache</td>
289+
</tr>
278290
<tr>
279291
<td><code>pjax:end</code></td>
280292
<td></td>

jquery.pjax.js

+3
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

+50
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)