newBike
newBike

Reputation: 15002

How to Component's methods with other controllers and components

I have perRoundROI,perRoundROIWithCoverage methods in BarChartComponent,

How to share the two functions with others ?

In other words, move the two methods in higher level so that other components can call it.

How to refactor it ?

How to call the 2 methods after refactoring

App.ToolsController = Ember.ArrayController.extend({
  ....  
});

App.BarChartComponent = Ember.Component.extend({

  perRoundROI:function(d){
    return d.manualHours - d.autoHours;
  },
  perRoundROIWithCoverage:function(d){
    return (d.coverageerage * d.manualHours) - d.autoHours;
  },

  get_tool_data: function(d, with_coverage){
    call perRoundROI
    call perRoundROIWithCoverage
  },
  ...
});

Upvotes: 0

Views: 48

Answers (1)

lcoq
lcoq

Reputation: 10726

Just create an Ember.Mixin.

Each class who needs its methods simply extends it:

App.ROICalculator = Ember.Mixin.create({
  perRoundROI: function(d) { /* code omitted */ },
  perRoundROIWithCoverage: function(d) { /* code omitted */ }
});

App.ToolsController = Ember.ArrayController.extend(App.ROICalculator, { 
  /* code omitted */ 
});

App.BarChartComponent = Ember.Component.extend(App.ROICalculator, {
  /* code omitted */
});

Upvotes: 1

Related Questions