You need to sign in to do that
Don't have an account?
ZG
How to call the setCallback function with a callback function
Hello, I am new to Lightning and am going through the Lightning Components Basics module.
In the expensesHelper.js file would like to find out if it's possible to move the callback function definition inside the setCallback function to its own function and pass it in the arguments.
Instead of this:
I would like to move the callback function defintion out into its own function like this:
WIth the code above I am getting the run-time error: "Error in $A.getCallback() [component is not defined]
Callback failed: apex://ExpensesController/ACTION$saveExpense
Failing descriptor: {c:expenses}"
I added a console log to make sure that "this.cbackfunction" exists and it does:
var state = response.getState();
var expenses = component.get("v.expenses");
expenses.push(response.getReturnValue()); component.set("v.expenses", expenses);
}
So, the cback function is visible from within the createExpense function.
Any ideas on how to call setCallback with a callback function?
In the expensesHelper.js file would like to find out if it's possible to move the callback function definition inside the setCallback function to its own function and pass it in the arguments.
Instead of this:
({ createExpense: function(component, expense) { var action = component.get("c.saveExpense"); action.setParams({ "expense": expense }); action.setCallback(this, function(response){ var state = response.getState(); if (state === "SUCCESS") { var expenses = component.get("v.expenses"); expenses.push(response.getReturnValue()); component.set("v.expenses", expenses); } }) $A.enqueueAction(action); }, })
I would like to move the callback function defintion out into its own function like this:
({ createExpense: function(component, expense) { var action = component.get("c.saveExpense"); action.setParams({ "expense": expense }); action.setCallback(this, this.cbackfunction); $A.enqueueAction(action); }, cbackfunction: function(response){ var state = response.getState(); if (state === "SUCCESS") { var expenses = component.get("v.expenses"); expenses.push(response.getReturnValue()); component.set("v.expenses", expenses); } } })
WIth the code above I am getting the run-time error: "Error in $A.getCallback() [component is not defined]
Callback failed: apex://ExpensesController/ACTION$saveExpense
Failing descriptor: {c:expenses}"
I added a console log to make sure that "this.cbackfunction" exists and it does:
console.log("this.cbackfunction: " + this.cbackFunction);this.cbackfunction: function (response) {
var state = response.getState();
var expenses = component.get("v.expenses");
expenses.push(response.getReturnValue()); component.set("v.expenses", expenses);
}
So, the cback function is visible from within the createExpense function.
Any ideas on how to call setCallback with a callback function?
"
You can move the body into another method, but you need to put a method inside the callback to call the other method to preserve the parameter:
"