Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Created June 26, 2017 17:17
Show Gist options
  • Save Sunil02kumar/ad6e8f5106b7655a0f7f21d49de97e6e to your computer and use it in GitHub Desktop.
Save Sunil02kumar/ad6e8f5106b7655a0f7f21d49de97e6e to your computer and use it in GitHub Desktop.
Firing component event from VF Page in lightning
<aura:event type="COMPONENT" description="Event template" >
<aura:attribute name="valuePassedFromEvent" type="string"/>
</aura:event>
<apex:page standardcontroller="Account" sidebar="false">
<apex:includeLightning />
<div id="compContainer" />
<!--Here here skforce in script is my org namespace. if you are not using namespace in your org,
then simply use c. like c:VFPageApp, c:VFPageComponent etc-->
<script>
$Lightning.use("skforce:VFPageApp", function() {
$Lightning.createComponent("skforce:VFPageComponent",
{PassesValueFromVF:"{!JSENCODE(Account.Name)}"},
"compContainer",
function(cmp) {
console.log("skforce:VFPageApp loaded successfully in VF page");
//if you register event in component then use below syntax
var eve = cmp.getEvent("cmpEvent");
eve.setParams(
{
valuePassedFromEvent:"{!JSENCODE(Account.BillingCountry)}"
});
console.log("firing event from VF page");
eve.fire();
console.log("event firing from VF page completed");
}
);
});
</script>
</apex:page>
<aura:application access="GLOBAL" extends="ltng:outApp">
<c:VFPageComponent />
</aura:application>
<aura:component >
<aura:attribute name="valueRecievedFromEvent" type="string"/>
<aura:attribute name="PassesValueFromVF" type="string"/>
<aura:registerEvent name="cmpEvent" type="c:EventForVFPage"/>
<aura:handler name="cmpEvent" event="c:EventForVFPage" action="{!c.eventHandlerFromVF}"/>
Account name Passed through component attribute from VF Page: <b>{!v.PassesValueFromVF}</b>
<br/>
Account billing country Passed from event: <b>{!v.valueRecievedFromEvent}</b>
</aura:component>
({
eventHandlerFromVF : function(component, event, helper) {
console.log("event captured in component controller in eventHandlerFromVF function");
var passedValue=event.getParam("valuePassedFromEvent");
console.log("**passedValue through event:"+passedValue);
component.set("v.valueRecievedFromEvent",passedValue);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment