Last active
February 27, 2020 03:56
-
-
Save douglascayers/13db5714c3cae0cbcacd252fba8c127a to your computer and use it in GitHub Desktop.
Lightning Component helper js methods for invoking apex methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
someMethod : function( component, event, helper ) { | |
var helper = this; | |
helper.callAction( component, 'c.someApexMethod', { | |
'someParam' : component.get( 'v.someAttribute' ) | |
}).then( $A.getCallback( function( data ) { | |
component.set( 'v.anotherAttribute', data ); | |
})).catch( $A.getCallback( function( errors ) { | |
helper.logActionErrors( component, errors ); | |
})); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
showSpinner : function( component ) { | |
$A.util.removeClass( component.find( 'spinner' ), 'slds-hide' ); | |
}, | |
hideSpinner : function( component ) { | |
$A.util.addClass( component.find( 'spinner' ), 'slds-hide' ); | |
}, | |
navigateToRecord : function( recordId ) { | |
console.log( 'navigating to record: ' + recordId ); | |
var event = $A.get( 'e.force:navigateToSObject' ); | |
if ( event ) { | |
event.setParams({ | |
'recordId' : recordId | |
}).fire(); | |
} else if ( ( typeof sforce !== 'undefined' ) && ( typeof sforce.one !== 'undefined' ) ) { | |
sforce.one.navigateToSObject( recordId ); | |
} else { | |
window.location.href = '/' + recordId; | |
} | |
}, | |
navigateToURL : function( url ) { | |
console.log( 'navigating to url: ' + url ); | |
var event = $A.get( 'e.force:navigateToURL' ); | |
if ( event ) { | |
event.setParams({ | |
'url' : url | |
}).fire(); | |
} else if ( ( typeof sforce !== 'undefined' ) && ( typeof sforce.one !== 'undefined' ) ) { | |
sforce.one.navigateToURL( url ); | |
} else { | |
window.location.href = url; | |
} | |
}, | |
callAction : function( component, actionName, params ) { | |
var helper = this; | |
var p = new Promise( function( resolve, reject ) { | |
helper.showSpinner( component ); | |
var action = component.get( actionName ); | |
if ( params ) { | |
action.setParams( params ); | |
} | |
action.setCallback( helper, function( response ) { | |
helper.hideSpinner( component ); | |
if ( component.isValid() && response.getState() === 'SUCCESS' ) { | |
resolve( response.getReturnValue() ); | |
} else { | |
console.error( 'Error calling action "' + actionName + '" with state: ' + response.getState() ); | |
reject( response.getError() ); | |
} | |
}); | |
$A.enqueueAction( action ); | |
}); | |
return p; | |
}, | |
logActionErrors : function( component, errors ) { | |
if ( errors ) { | |
if ( errors.length > 0 ) { | |
for ( var i = 0; i < errors.length; i++ ) { | |
console.error( 'Error: ' + errors[i].message ); | |
} | |
} else { | |
console.error( 'Error: ' + errors ); | |
} | |
} else { | |
console.error( 'Unknown error' ); | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment