1

Trying to do a if statement in a client side function. If the statement is true, you proceed with the action. If not, you exit from the function. Is this possible?

doInit : function(component, event, helper) {
    if (this is true) {
        var action = component.get("c.getPickListValuesFromSubjects");
        action.setCallback(this, function(response) {            
            var state = response.getState();
            if (state === "SUCCESS") {
                var pickListValues = response.getReturnValue();
                component.set("v.optionsForSubject", pickListValues);
            } 
            else {
                console.log(state);
            }
        });     
        $A.enqueueAction(action);}
    exit;
},
2
  • why are you putting exit? if If logic is not matching it will not execute that part and exit from Init function
    – Himanshu
    Commented Apr 20, 2018 at 23:56
  • I see. Makes sense Himanshu! Maybe I was overthinking the problem a little... =/
    – Jason Fung
    Commented Apr 21, 2018 at 0:32

1 Answer 1

4

You need to leverage the return statement in your javascript.

doInit: function(component, event, helper) {
    if (!(this is true)) { // if the value is not true
        return;            // return control back to the caller
    }                      // do not proceed any further

    var action = component.get("c.getPickListValuesFromSubjects");
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var pickListValues = response.getReturnValue();
            component.set("v.optionsForSubject", pickListValues);
        } else {
            console.log(state);
        }
    });
    $A.enqueueAction(action);
}
1
  • Yes that would work too Mark. But I like Himanshu's suggestion better because it helps keeping my code clean and concise. Thanks for your suggestion anyway!
    – Jason Fung
    Commented Apr 21, 2018 at 0:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .