Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4870

Reusing plain old javascript in component classes

$
0
0

Hopefully a straight forward question: where should I put plain JavaScript that I can use between a few different components?

I have a couple of different component that have a isActive property based on whether the current route maps to a route passed ot the component. Something like:

export default class MenuItemSimpleComponent extends Component {
  @service router;
  
  get isActive() {
    const route = this.args.route.split('.');
    const curr = this.router.currentRouteName.split('.');

    // from here to the end is basically POJS and could be in a reusable function somewhere
    const stripThese = ['new', 'edit', 'index', 'modal'];
    if (stripThese.includes(curr[curr.length - 1])) { curr.pop(); }

    return pluralize(route[route.length - 1]) == pluralize(curr[curr.length - 1]);
  }

To rephrase: what is the best practices for extracting out a reusable chunk of POJS in Ember 4?

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 4870

Trending Articles