Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filters not available in JS Class data method #467

Closed
corderophi678 opened this issue Mar 27, 2019 · 4 comments
Closed

Filters not available in JS Class data method #467

corderophi678 opened this issue Mar 27, 2019 · 4 comments
Labels
education waiting-to-close Issue that is probably resolved, waiting on OP confirmation.

Comments

@corderophi678
Copy link

The docs show this example, under the "Permalink Function Using a Filter" section:

class Test {
  data() {
    return {
      title: "This is my blog post title",
      // Writes to "/this-is-my-blog-post-title/index.html"
      permalink: data => `/${this.slug(data.key)}/`
    };
  }

  render(data) { /* … */ }
}

module.exports = Test;

But running this example yields an error:

> Having trouble rendering 11ty.js template ./index.11ty.js (TemplateContentRenderError)
> this.slug is not a function (TypeError):
    TypeError: this.slug is not a function

It appears that filters are not actually available in a class's data method. They only seem to be available within the render method (this seems to be confirmed by console.logging this in both the data method and the render method, which logging yields different output).

(Incidentally, the example above's permalink key references data.key, but that should probably be data.title. I was going to submit a PR for that little fix, but thought the availability of filters on the data method deserved primacy.)

@zachleat
Copy link
Member

You may be falling prey to arrow functions not having their own this. Try switching to a non arrow function for your permalink and it might work!

@zachleat
Copy link
Member

zachleat added a commit to 11ty/11ty-website that referenced this issue Mar 29, 2019
@zachleat
Copy link
Member

Ha! Rather we fell prey to that.

I updated the example here https://www.11ty.io/docs/languages/javascript/#permalink-function-using-a-filter

Thanks!

@zachleat zachleat added the waiting-to-close Issue that is probably resolved, waiting on OP confirmation. label Mar 29, 2019
@corderophi678
Copy link
Author

😊 Thanks!

zachleat added a commit to 11ty/11ty-website that referenced this issue Mar 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
education waiting-to-close Issue that is probably resolved, waiting on OP confirmation.
Projects
None yet
Development

No branches or pull requests

2 participants