Skip to content

Commit 3255689

Browse files
committed
pat-inject: next-href wasn't working as advertised.
Add the ability to use a URL for next-href and let that be properly set.
1 parent 9322a1c commit 3255689

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Add the class `modal-active` to the `body` element whenever a modal is in the DOM.
1010
- New pattern: `pat-tabs`. See the relevant documentation.
1111
- Bugfix: `pat-validation` still validates removed clones from `pat-clone`.
12+
- Let the next-href option of pat-inject work as advertised.
1213
- #436 Remove `pat-bumper` restriction that scroll container must be the direct parent.
1314

1415
## 2.0.12 - Oct. 9, 2015

jshintrc-tests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"white": false,
1212
"quotmark": false,
1313
"predef": [
14+
"Event",
1415
"Markdown",
1516
"Modernizr",
1617
"define",

src/pat/inject/inject.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ define([
4343
}
4444
$el.data("pat-inject", cfgs);
4545

46-
// In case next-href is specified the anchor's href will
47-
// be set to it after the injection is triggered. In case
48-
// the next href already exists, we do not activate the
49-
// injection but instead just change the anchors href.
50-
var $nexthref = $(cfgs[0].nextHref);
51-
if ($el.is("a") && $nexthref.length > 0) {
52-
log.debug("Skipping as next href already exists", $nexthref);
53-
// XXX: reconsider how the injection enters exhausted state
54-
return $el.attr({href: (window.location.href.split("#")[0] || "") + cfgs[0].nextHref});
46+
if (cfgs[0].nextHref && cfgs[0].nextHref.indexOf('#') === 0) {
47+
// In case the next href is an anchor, and it already
48+
// exists in the page, we do not activate the injection
49+
// but instead just change the anchors href.
50+
51+
// XXX: This is used in only one project for linked
52+
// fullcalendars, it's sanity is wonky and we should
53+
// probably solve it differently.
54+
if ($el.is("a") && $(cfgs[0].nextHref).length > 0) {
55+
log.debug("Skipping as next href is anchor, which already exists", cfgs[0].nextHref);
56+
// XXX: reconsider how the injection enters exhausted state
57+
return $el.attr({href: (window.location.href.split("#")[0] || "") + cfgs[0].nextHref});
58+
}
5559
}
5660

5761
switch (cfgs[0].trigger) {
@@ -118,7 +122,7 @@ define([
118122
cfgs = $sub.data("pat-inject");
119123

120124
// store the params of the subform in the config, to be used by history
121-
$(cfgs).each(function(i, v) {v.params = $.param($sub.serializeArray())});
125+
$(cfgs).each(function(i, v) {v.params = $.param($sub.serializeArray());});
122126

123127
try {
124128
$el.trigger("patterns-inject-triggered");
@@ -407,9 +411,10 @@ define([
407411
inject._performInjection.apply(this, [$el, sources$[idx], cfg, ev.target]);
408412
});
409413
});
410-
if (cfgs[0].nextHref) {
411-
$el.attr({href: (window.location.href.split("#")[0] || "") +
412-
cfgs[0].nextHref});
414+
if (cfgs[0].nextHref && $el.is("a")) {
415+
// In case next-href is specified the anchor's href will
416+
// be set to it after the injection is triggered.
417+
$el.attr({href: cfgs[0].nextHref});
413418
inject.destroy($el);
414419
}
415420
$el.off("pat-ajax-success.pat-inject");

src/pat/inject/tests.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ define(["pat-inject", "pat-utils"], function(pattern, utils) {
1515
$("#lab").remove();
1616
});
1717

18+
describe("The next-href argument", function() {
19+
afterEach(function() {
20+
$("#lab").empty();
21+
});
22+
23+
it("allows you to specify the href to be applied to the clicked element after injection", function () {
24+
var $a = $("<a class=\"pat-inject\" data-pat-inject=\"next-href: http://patternslib.com\" href=\"/src/pat/inject/inject-sources.html#pos-1\">link</a>");
25+
var $div = $("<div id=\"pos-1\" />");
26+
$("#lab").append($a).append($div);
27+
var dummy_event = {
28+
'jqxhr': {'responseText': 'foo'},
29+
'isPropagationStopped': function () { return true; }
30+
};
31+
var cfgs = pattern.extractConfig($a, {});
32+
pattern.verifyConfig(cfgs, $a);
33+
pattern._onInjectSuccess($a, cfgs, dummy_event);
34+
expect(cfgs[0].nextHref).toBe('http://patternslib.com');
35+
expect($a.attr("href")).toBe('http://patternslib.com');
36+
});
37+
38+
});
39+
1840
describe("The loading-class argument", function() {
1941

2042
afterEach(function() {

0 commit comments

Comments
 (0)