astralwest
10/10/2018 - 2:55 PM

Why rewrite response state handling when you can call a function to deal with that for you. This is that function. Paste it in your helper a

Why rewrite response state handling when you can call a function to deal with that for you. This is that function. Paste it in your helper and call it from your helper methods or choose a better code reuse method; https://developer.salesforce.com/blogs/developer-relations/2016/12/lightning-components-code-sharing.html. The helper method then only need deal with supplying the return value in a form helpful to the client side controller.

//  getHoursLoggedToday:function(component,event,callback){
//      this.enqueueAction(component,"c.getHoursLoggedToday",{success:function(resoponse){callback(response.getReturnValue());}});
//  },

enqueueAction:function(component,method,detail){
	var action=component.get(method);
	if(detail==undefined){
		console.log("helper.enqueueAction: detail is undefined.");
		return;
	}
	if(detail.parameters!=undefined)
		action.setParams(detail.parameters);

	action.setCallback(this,function(response){
		var state=response.getState();
		if(state=="SUCCESS")
		{
			if(detail.success!=undefined)
				detail.success(response);
		}
		else if(state=="INCOMPLETE")
		{
			if(detail.incomplete!=undefined)
				detail.incomplete(response);
		}
		else if(state=="ERROR")
		{
			if(detail.error!=undefined)
				detail.error(response,response.getError());
			else
			{
				var errors=response.getError();
				if(errors)
				{
					if(errors[0]&&errors[0].message)
						console.log("Error: "+errors[0].message);
				}
				else
					console.log("Error: Unknown.");
			}
		}
	});
	// Future: setstorable, abortable, background flags here.
	$A.enqueueAction(action);
}