1

I encounter an error and i think the problem is the fact that apex is asynchronous. Here the explanation.

From my component i call the method doAction:

<aura:attribute name="value" type="List"/>
<aura:handler name="init" action="{!c.doAction}" value="{!this}"/>

This method call an apex class to select the highest value in my table with a simple select MAX

doAction : function(component, event, helper) {
    console.log("getMostWished");
    var action = component.get("c.getMostWished");
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (component.isValid() && state === "SUCCESS") {
            console.log("MostWished");
            component.set("v.value", response.getReturnValue())
        }
        else {
            console.log("getMostWished failed with state: " + state);
        }
    });
    $A.enqueueAction(action);

So this value must be stored into 'v.value' but later in my code when I use it, v.value is null.

On the console browser, the console.log("MostWished"); is displayed in last but some logs who are later in the code are displayed first.

I use v.value in a second component

<c:barGraph products="{!prod}" value="{!v.value}"/>

In this component, i call findMax method

<aura:attribute name="value" type="Object"/>
<aura:handler name="init" action="{!c.findMax}" value="{!this}"/>

for the moment findMax is just here as debug with console.log. Those console.log are displayed before the MostWished above and the console.dir say the v.value is null.

findMax : function(component, event, helper) {
        var val = component.get("v.value");
        console.log('ICI');
        console.dir(val);
    }

does exist a way to wait for the value, stop the execution a moment or tell my program 'Wait the end of method doAction'?

1

It's null because the init function is called before the data gets back from the server. Most likely, you want to set a change handler instead:

<aura:attribute name="value" type="Object" />
<aura:handler name="change" value="{!v.value}" action="{!c.findMax}" />

Whenever a change is detected in value, your findMax function should be called. See Detecting Data Changes with Change Handlers and aura:valueChange.

  • Thanks a lot, exactly what i needed. and thank you to have shared the good corresponding documentation with your answer. – Tellimi Dec 9 '16 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.