Invoke custom ember helper in code - ember.js

I have a custom helper 'myHelperFunction' and I want to call the method in code, how can i do that?
import Ember from 'ember';
export function myHelperFunction(params) {
return myData;
}
export default Ember.Helper.helper(myHelperFunction);

My solution was:
my-function.js (Helper)
import Ember from 'ember';
export default Ember.Helper.helper(function(params) {
...
return myData;
});
and then:
import myFunction from '../helpers/my-function';
...
myFunction.compute([param1, param2])
...

You have to import it using import keyword in file where you want to use it:
import myHelperFunction from '../helpers/my-helper-function';
let result = myHelperFunction();

Related

In Ember, how can I access the custom inflector defined in my initializers?

I'm defining custom inflector rules in app/initializers/custom-inflector-rules like so:
// app/initializers/custom-inflector-rules
import Inflector from 'ember-inflector';
export function initialize(/* application */) {
const inflector = Inflector.inflector;
// Tell the inflector that the plural of "campus" is "campuses"
inflector.irregular('campus', 'campuses');
}
export default {
name: 'custom-inflector-rules',
initialize
};
How can I access my initialized inflector with these custom rules within my application?
For example, in a serializer file, I want to be able to call pluralize like so:
import ??? as Inflector from ???
Inflector.inflector.pluralize("campus"); // campuses
You know the import already from your initializer:
import Inflector from 'ember-inflector';
Then you can just do Inflector.inflector.pluralize('...').
You can also just directly import pluralize for the default inflector:
import { singularize, pluralize } from 'ember-inflector';
pluralize('...');
This is exactly Inflector.inflector.pluralize as you can see in the source
There is even a handlebars helper:
{{pluralize "taco"}}
The complete docs are in the github readme.

Ember.js: access the current component from helper

Looking for any solution (even dirty hacks) to access the current component from a custom helper.
import Ember from 'ember';
export default Ember.Helper.extend({
compute() {
... who is computing me? ...
}
});
Simply pass this to the helper.
Say the code example you mentioned is for format-currency helper, so you can pass the context like {{format-currency value this}}
And in the helper you can access it like:
import Ember from 'ember';
export default Ember.Helper.extend({
compute([value, container]) {
//... who is computing me? ...
// container is computing you
}
});

fn.apply is not a function - trying to create a Handlebars helper

I create the file app/helpers/test-helper.js:
import Ember from 'ember';
export default Ember.Handlebars.registerBoundHelper('test-helper', function() {
return "Works!";
});
And in the template:
{{test-helper}}
And I get the above error in the console. What am I doing wrong here?
I wrestled with this for awhile myself. The trick is to use makeBoundHelper instead of registerBoundHelper
import Ember from 'ember';
export default Ember.Handlebars.makeBoundHelper('test-helper', function() {
return "Works!";
});
Here's a link to the source code

Using Ember CLI and the ES6 syntax `export default` how do I know if the export is an Ember.Object

When I am using Ember CLI and the ES6 syntax export default something, I would really like to know if the something from my export was an Ember.Object. What could I check on the object to ensure that is an Ember.Object or extends Ember.Object?
// app/repos/wat.js
import Ember from "ember";
var wat = Ember.Object.extend({
doSomething: function() {
console.log("do stuff");
}
});
export default wat;
// app/utils/wat.js
import Ember from "ember";
import wat from "../repos/wat";
console.log(wat instanceof Ember.Object);
// console.log above returns false
Just use instanceof:
import SomeObject from 'some-place';
var isEmberObject = SomeObject instanceof Ember.Object;
EDIT: Sorry, I answered too quickly. That version only works for instances, not classes. To check if a class extends Ember.Object, you can do this:
import SomeClass from 'some-other-place';
var isEmberClass = SomeClass.prototype instanceof Ember.Object
You can also use isPrototypeOf:
import SomeClass from 'some-other-place';
var isEmberClass = Ember.Object.prototype.isPrototypeOf(SomeClass.prototype);

Ember CLI Test Helpers

Can somebody point me to a resource on how to implement a test helper with ember-cli?
Or else a simple explanation?
I know the helpers go in the test/helpers directory, but how do you load them into the integration tests?
Thanks
The only way I found to do this is:
// tests/helpers/controller.js
import Ember from 'ember';
Ember.Test.registerHelper('controller', function (app, name) {
return app.__container__.lookup('controller:' + name);
});
then in my acceptance test:
// acceptance/index-test.js
import Ember from 'ember';
// import our helper (this might be done within helpers/start-app.js to always import all helpers)
import '../helpers/controller';
import startApp from '../helpers/start-app';
// your tests using the helper(s)
But there might be some better way of doing.