Picture of Martin Sandberg
Calling a function of a different component?
by Martin Sandberg - Wednesday, 1 February 2017, 2:44 PM
 

Hi,

Been away for too long from Adapt. Have been too busy with Totara projects but have finally an interresting Adapt project to work with.

The main component for this will be a graph that is drawn on canvas based on JSON files loaded in the component.

Unfortunately this graph is proprietary to the customer so I can't give it to the community. Would have loved to but can't.

Anyway, we will also need to create multiple other components that will manipulate the data in a graph component.

Can I in a function for component 2 find and call a funtion in component 1 (based on the id of the component).

/ Martin

 

Picture of Dan Storey
Re: Calling a function of a different component?
by Dan Storey - Thursday, 2 February 2017, 3:01 PM
 

Hi Martin,

You should be able to use triggers do accomplish what you're asking. Couple of ways of doing this but something like this should work.

First set up the method in your view:

loadChartData: function(event, param1, param2) {},

Then setup the listener in the postRender method:

this.$el.on("triggerChart", this.loadChartData.bind(this));

You can then trigger the loadChartData method by calling:

$(".your-component-id").trigger(param1, param2); 

And in case there's any doubt - I didn't mistake the . for a # in the above call as Adapt assigns the id in the json as the class of the component element. Hope that helps!

Picture of Martin Sandberg
Re: Calling a function of a different component?
by Martin Sandberg - Friday, 3 February 2017, 8:00 AM
 

Thanks Dan,

Tested to call a function in another component like this.

Worked fine... now I just need to create the function I need.

Picture of Dan Storey
Re: Calling a function of a different component?
by Dan Storey - Friday, 3 February 2017, 8:41 AM
 

Brill!