Skip to content

Commit

Permalink
Pagination: bind this to before callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaza committed Mar 17, 2021
1 parent 8a51627 commit 78b525b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Pagination {
typeof this.data.pagination.before === "function"
) {
// we don’t need to make a copy of this because we already .filter() above
result = this.data.pagination.before(result);
result = this.data.pagination.before.bind(this)(result);
}

if (this.data.pagination.reverse === true) {
Expand Down
14 changes: 14 additions & 0 deletions test/PaginationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,20 @@ test("Pagination `before` Callback with a Filter", async (t) => {
t.deepEqual(templates[0].data.myalias, "item2");
});

test("Pagination `before` Callback with a Filter using `this` data", async (t) => {
let tmpl = getNewTemplate(
"./test/stubs/paged/paged-before-filter-this.njk",
"./test/stubs/",
"./dist"
);

let data = await tmpl.getData();
let templates = await tmpl.getTemplates(data);
t.is(templates.length, 2);
t.deepEqual(templates[0].data.pagination.items, ["foo"]);
t.deepEqual(templates[1].data.pagination.items, ["woo"]);
});

test("Pagination `before` Callback with `reverse: true` (test order of operations)", async (t) => {
let tmpl = getNewTemplate(
"./test/stubs/paged/paged-before-and-reverse.njk",
Expand Down
14 changes: 14 additions & 0 deletions test/stubs/paged/paged-before-filter-this.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---js
{
fooData: "moo",
pagination: {
data: "items",
size: 1,
before: function(data) {
return data.filter(item => item !== this.data.fooData);
}
},
items: ["foo", "moo", "woo"]
}
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>

0 comments on commit 78b525b

Please sign in to comment.