jeudi 2 juillet 2015

Pass function to an Ember component

Is it possible to pass a function to a component? I'm trying to move my code away from views and towards components to keep my Ember app moving forward (see here), and I've got a bunch of commonly used functions I use throughout my site. I am not sure how to call them from within a component. I had put them in their own controller in my older code, but I hear it's sloppy or unwise to pass controllers to a component.

For example in a component handlebars file, I'm considering this:

{{a-cool-component 
  property1=someValue
  function1=controllers.utils.doSomethingRepetitious 
}}

So that in my component javascript code I can do something like this...

FacetListItemComponent = Ember['default'].Component.extend({
  property1: null
  property2: null
  function1: null 
  didInsertElement: function() {
    //... do stuff here
    this.set('property2', this.function1(this.get('property1')));
  }
);
exports['default'] = FacetListItemComponent;

I tried it in a component I'm building, but the function1 came across as undefined.

Any suggestions?

I'm using Ember 1.11 on Ember-CLI 0.2.7

Bryan




Aucun commentaire:

Enregistrer un commentaire