From e6a35f38e4988873d83218dd14435a6049d8a06e Mon Sep 17 00:00:00 2001 From: Peter Lada Date: Wed, 21 Jan 2015 00:11:59 -0800 Subject: [PATCH 1/2] attach data to pjaxified links so they can't be pjaxified twice. add simple refresh method --- index.js | 2 ++ lib/proto/attach-link.js | 2 ++ lib/proto/parse-element.js | 5 ++++- lib/proto/refresh.js | 6 ++++++ 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 lib/proto/refresh.js diff --git a/index.js b/index.js index 65e8326..e17eef1 100644 --- a/index.js +++ b/index.js @@ -44,6 +44,8 @@ Pjax.prototype = { parseDOM: require("./lib/proto/parse-dom.js"), + refresh: require("./lib/proto/refresh.js"), + attachLink: require("./lib/proto/attach-link.js"), forEachSelectors: function(cb, context, DOMcontext) { diff --git a/lib/proto/attach-link.js b/lib/proto/attach-link.js index d8ca18d..f0ad789 100644 --- a/lib/proto/attach-link.js +++ b/lib/proto/attach-link.js @@ -56,6 +56,8 @@ var linkAction = function(el, event) { module.exports = function(el) { var that = this + el.setAttribute("data-pjax-enabled", "true"); + on(el, "click", function(event) { linkAction.call(that, el, event) }) diff --git a/lib/proto/parse-element.js b/lib/proto/parse-element.js index a6311d2..4f7a8cd 100644 --- a/lib/proto/parse-element.js +++ b/lib/proto/parse-element.js @@ -1,7 +1,10 @@ module.exports = function(el) { switch (el.tagName.toLowerCase()) { case "a": - this.attachLink(el) + // only attach link if el does not already have link attached + if (!el.getAttribute("data-pjax-enabled")) { + this.attachLink(el) + } break case "form": diff --git a/lib/proto/refresh.js b/lib/proto/refresh.js new file mode 100644 index 0000000..39c271b --- /dev/null +++ b/lib/proto/refresh.js @@ -0,0 +1,6 @@ + +var parseDom = require("./parse-dom") + +module.exports = function(el) { + parseDom(document); +} From 9a86044f90399c714867c4155bafd18e57199da3 Mon Sep 17 00:00:00 2001 From: Peter Lada Date: Wed, 28 Jan 2015 18:27:16 -0800 Subject: [PATCH 2/2] changed existing refresh method to reload. changed the data check to trigger a refresh on the element based on the already bound data attr --- lib/proto/attach-link.js | 6 ++---- lib/proto/parse-element.js | 2 +- lib/{refresh.js => reload.js} | 0 tests/index.html | 10 +++++----- tests/lib/proto/attach-link.js | 6 +++--- 5 files changed, 11 insertions(+), 13 deletions(-) rename lib/{refresh.js => reload.js} (100%) diff --git a/lib/proto/attach-link.js b/lib/proto/attach-link.js index f0ad789..d53e351 100644 --- a/lib/proto/attach-link.js +++ b/lib/proto/attach-link.js @@ -44,8 +44,8 @@ var linkAction = function(el, event) { // don’t do "nothing" if user try to reload the page by clicking the same link twice if (el.href === window.location.href.split("#")[0]) { - el.setAttribute(attrClick, "refresh") - this.refresh() + el.setAttribute(attrClick, "reload") + this.reload() return } @@ -56,8 +56,6 @@ var linkAction = function(el, event) { module.exports = function(el) { var that = this - el.setAttribute("data-pjax-enabled", "true"); - on(el, "click", function(event) { linkAction.call(that, el, event) }) diff --git a/lib/proto/parse-element.js b/lib/proto/parse-element.js index 4f7a8cd..736fdd7 100644 --- a/lib/proto/parse-element.js +++ b/lib/proto/parse-element.js @@ -2,7 +2,7 @@ module.exports = function(el) { switch (el.tagName.toLowerCase()) { case "a": // only attach link if el does not already have link attached - if (!el.getAttribute("data-pjax-enabled")) { + if (!el.hasAttribute('data-pjax-click-state')) { this.attachLink(el) } break diff --git a/lib/refresh.js b/lib/reload.js similarity index 100% rename from lib/refresh.js rename to lib/reload.js diff --git a/tests/index.html b/tests/index.html index 4c8c6b6..a9ef972 100644 --- a/tests/index.html +++ b/tests/index.html @@ -12091,8 +12091,8 @@ var linkAction = function(el, event) { // don’t do "nothing" if user try to reload the page by clicking the same link twice if (el.href === window.location.href.split("#")[0]) { - el.setAttribute(attrClick, "refresh") - this.refresh() + el.setAttribute(attrClick, "reload") + this.reload() return } @@ -12409,8 +12409,8 @@ tape("test attach link prototype method", function(t) { attachLink.call({ options: {}, - refresh: function() { - t.equal(a.getAttribute(attr), "refresh", "triggering exact same url refresh the page") + reload: function() { + t.equal(a.getAttribute(attr), "reload", "triggering exact same url reload the page") }, loadUrl: function() { t.equal(a.getAttribute(attr), "load", "triggering a internal link actually load the page") @@ -12444,7 +12444,7 @@ tape("test attach link prototype method", function(t) { a.href = internalUri trigger(a, "click") - // see refresh defined above + // see reload defined above a.href = window.location.protocol + "//" + window.location.host + "/internal" trigger(a, "click") diff --git a/tests/lib/proto/attach-link.js b/tests/lib/proto/attach-link.js index aa66a88..29202c1 100644 --- a/tests/lib/proto/attach-link.js +++ b/tests/lib/proto/attach-link.js @@ -13,8 +13,8 @@ tape("test attach link prototype method", function(t) { attachLink.call({ options: {}, - refresh: function() { - t.equal(a.getAttribute(attr), "refresh", "triggering exact same url refresh the page") + reload: function() { + t.equal(a.getAttribute(attr), "reload", "triggering exact same url reload the page") }, loadUrl: function() { t.equal(a.getAttribute(attr), "load", "triggering a internal link actually load the page") @@ -48,7 +48,7 @@ tape("test attach link prototype method", function(t) { a.href = internalUri trigger(a, "click") - // see refresh defined above + // see reload defined above a.href = window.location.protocol + "//" + window.location.host + "/internal" trigger(a, "click")