force:editRecord
event works fine in my lightning app, but the same does not work in the visual force page. I am aware that force:editRecord
is handled by one app container but I am looking for work around here like using $A.eventService.addHandler
.
Visualforce page
<apex:page showheader="false" sidebar="false" standardStylesheets="false">
<html>
<body>
<apex:includeLightning />
<div id="lightning"/>
<script>
var myUserContext = "{!$User.UITheme}";
$Lightning.use("c:TestApp", function() {
$Lightning.createComponent("c:xyz",
{},
"lightning",
function(cmp) {
$A.eventService.addHandler({
event:'force:editRecord',
handler: function(event {
varlexOrigin="https://xyz.lightning.force.com";
var message=
{type: "EventFromVF",
message: event.getParams().recordId};
parent.postMessage(message, lexOrigin);
}
});
}
);
});
</script>
</body>
</html>
</apex:page>
lightning source from edit record being called
======================
edit:function(component, event, helper) {
var editRecordEvent = $A.get("e.force:editRecord");
editRecordEvent.setParams({"recordId": component.get("v.record.Id")});
editRecordEvent.fire();
Listener controller and component
<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
<aura:attribute name="vfHost" type="String"
default="xyz.visual.force.com"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<iframe aura:id="vfFrame" src="{!'https://' + v.vfHost + '/apex/xyzpage'}" />
</aura:component>
({
doInit : function(component) {
debugger;
console.log('hello listener');
var vfOrigin = "https://" + component.get("v.vfHost");
window.addEventListener("message", function(event) {
if ( (event.data.type) && (event.data.type=='EventFromVF') )
if (event.origin !== vfOrigin) {
// Not the expected origin: Reject the message!
return;
}
// Handle the message
var toastEvent = $A.get('e.force:editRecord');
toastEvent.setParams({
recordId: event.data.message
});
toastEvent.fire();
console.log(event.data);
}, false);
}
})
lightning application TestApp
<aura:application extends="ltng:outApp" access="GLOBAL" >
<aura:dependency resource="c:xyz" />
<aura:dependency resource="markup://force:editRecord" type="EVENT"/>
</aura:application>