diff --git a/jquery.pjax.js b/jquery.pjax.js index 5cec3f28..485aa266 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -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(); } diff --git a/test/unit/pjax.js b/test/unit/pjax.js index 3b97dff8..1a74ae3a 100644 --- a/test/unit/pjax.js +++ b/test/unit/pjax.js @@ -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