mardi 30 juin 2015

In an Ember Component where should you put reusable code that isn't an action?

I have a standard component defined something like:

export default Ember.Component.extend({
  hideIfEmptyChange: function() {

    var thingOfInterest = ...;
    var otherThingOfInterest = ...;

    ...
    // Perform some logic
    ...

    // Perform some logic using thingOfInterest
    // Perform exactly the same logic using otherThingOfInterest
  }.observes('hideIfEmpty.@each')
});

I want to move the logic for the last two pieces into their own function to prevent writing out the same code twice just for two different variables.

I could write an action on my component and use this.send('myAction',...) - but would it be best practice to have this as an action if it isn't going to be used (or even usable) from the component's template? If you shouldn't do it this way then how should you?

My other thought was mixin's - but the code here will be completely specific to the component, so again this doesn't feel 100% right.




Aucun commentaire:

Enregistrer un commentaire