Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function applyFilters() {

pjax fires a number of events regardless of how its invoked.

All events are fired from the container, not the link was clicked.
All events are fired from the container, not the link that was clicked.

#### start and end

Expand All @@ -203,6 +203,7 @@ This pair events fire anytime a pjax request starts and finishes. This includes

* `pjax:beforeSend` - Fired before the pjax request begins. Returning false will abort the request.
* `pjax:send` - Fired after the pjax request begins.
* `pjax:beforeReplace` - Fired after the pjax request finishes, right before the content of the container is replaced.
* `pjax:complete` - Fired after the pjax request finishes.
* `pjax:success` - Fired after the pjax request succeeds.
* `pjax:error` - Fired after the pjax request fails. Returning false will prevent the the fallback redirect.
Expand Down
3 changes: 3 additions & 0 deletions jquery.pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ function pjax(options) {
} catch (e) { }

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

fire('pjax:beforeReplace', [container.contents, options])
context.html(container.contents)

// FF bug: Won't autofocus fields that are inserted via JS.
Expand Down Expand Up @@ -442,6 +444,7 @@ function onPjaxPopstate(event) {
container.trigger('pjax:start', [null, options])

if (state.title) document.title = state.title
container.trigger('pjax:beforeReplace', [contents, options])
container.html(contents)
pjax.state = state

Expand Down
50 changes: 50 additions & 0 deletions test/unit/pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,28 @@ if ($.support.pjax) {
})
})

asyncTest("triggers pjax:beforeReplace event from container", function() {
var frame = this.frame,
beforeContent = 'foo'

frame.$("#main")
.text(beforeContent)
.on("pjax:beforeReplace", function(event, contents, options) {
ok(event)
ok(contents)
equal($(event.target).text(), beforeContent)
equal(options.url, "hello.html")
})
frame.$("#main").on("pjax:success", function(event) {
notEqual($(event.target).text(), beforeContent)
start()
})

frame.$.pjax({
url: "hello.html",
container: "#main"
})
})

asyncTest("triggers pjax:success event from container", function() {
var frame = this.frame
Expand Down Expand Up @@ -828,6 +850,34 @@ if ($.support.pjax) {
})
})

asyncTest("popstate triggers pjax:beforeReplace event", function() {
var frame = this.frame,
originalContent = $(frame).html()

equal(frame.location.pathname, "/home.html")

frame.$('#main').on("pjax:complete", function() {
equal(frame.location.pathname, "/hello.html")
ok(frame.history.length > 1)

frame.$('#main').on('pjax:beforeReplace', function(event, contents, options) {
ok(event)
ok(contents)
equal(frame.location.pathname, "/home.html")
// Remember: the content hasn't yet been replaced.
notEqual($(event.target).html(), originalContent)
start()
})

goBack(frame, function() {})
})

frame.$.pjax({
url: "hello.html",
container: "#main"
})
})

// Test is fragile
asyncTest("no initial pjax:popstate event", function() {
var frame = this.frame
Expand Down