Skip to content

Commit

Permalink
Allow to change context in a pjax:beforeReplace handler
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonTyutin committed Sep 5, 2014
1 parent cc0dd17 commit 437ad6c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions jquery.pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ function pjax(options) {
state: pjax.state,
previousState: previousState
})
context.html(container.contents)
options.context.html(container.contents)

// FF bug: Won't autofocus fields that are inserted via JS.
// This behavior is incorrect. So if theres no current focus, autofocus
// the last field.
//
// http://www.w3.org/html/wg/drafts/html/master/forms.html
var autofocusEl = context.find('input[autofocus], textarea[autofocus]').last()[0]
var autofocusEl = options.context.find('input[autofocus], textarea[autofocus]').last()[0]
if (autofocusEl && document.activeElement !== autofocusEl) {
autofocusEl.focus();
}
Expand Down
23 changes: 23 additions & 0 deletions test/unit/pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,29 @@ if ($.support.pjax) {
})
})

asyncTest("change context node in pjax:beforeReplace event handler", function() {
var frame = this.frame,
targetContent = 'foo',
changedTarget = frame.$('ul > li:first')
changedTargetOriginalContent = changedTarget.text()

frame.$("#main")
.text(targetContent)
.on("pjax:beforeReplace", function(event, contents, options) {
options.context = changedTarget;
})
frame.$("#main").on("pjax:success", function(event) {
notEqual(changedTarget.text(), changedTargetOriginalContent)
equal($(event.target).text(), targetContent)
start()
})

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

asyncTest("triggers pjax:success event from container", function() {
var frame = this.frame

Expand Down

0 comments on commit 437ad6c

Please sign in to comment.