Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
Ember.js contains string helper

I'm looking to something like this in template file:

{{#contains 'invalid' item.name}}
  something
{{/contains}}

Basically check if the string contains a certain word

Not sure how the helper function would look like, but this is a random guess although I don't think this complies to above code.

import Helper from 'ember-helper';

export function containsHelper([substr, str]) {
 return str.contains(substr)
}

export default Helper.helper(containsHelper);

Something like that but how do I do it in a Ember.js helper such that I can use it in a template file?

Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You've guessed it pretty much right. This would be your implementation:

// app/helpers/contains.js

import { helper } from '@ember/component/helper';

function contains([substr, str]) {
  return str.contains(substr)
}

export default helper(contains);

and then in your template:

{{contains 'invalid' item.name}}

or rather

{{#if (contains 'invalid' item.name)}}
 your stuff
{{/if}}

Here's the doc

about 4 years ago · Juan Pablo Isaza Relatório

0

In addition to @Andrey's answer, I'm in-progress of getting https://github.com/glimmerjs/glimmer-vm/pull/1348 merged, which allows you to use "just plain functions" as helpers (RFC: https://github.com/emberjs/rfcs/pull/756)

There is a polyfill you can install: https://github.com/NullVoxPopuli/ember-functions-as-helper-polyfill

npm install ember-functions-as-helper-polyfill

and then you could do this:

export default class MyComponent extends Component {
  contains = (substr, str) => str.includes(substr);
}
{{#if (this.contains 'invalid' item.name)}}
  something
{{/if}}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda