Reputation: 359
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!
Upvotes: 0
Views: 1257
Reputation: 65173
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}}
Upvotes: 0
Reputation: 5949
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
Upvotes: 2