+ Start a Discussion
Bobby KnezevicBobby Knezevic 

Component initializer called twice

Hello, there. I'm working on very first Lightning application, and I'm noticing some strange behavior. I'm using a VF page with <apex:includeLightning /> and a script to load a component from an app like so:
 
$Lightning.use("c:OpportunityProductApp", function() {
    $Lightning.createComponent("c:PricebookSelector", {}, "pricebookSelector", function(cmp) {
        console.log('loaded component ...');
	    // do some stuff here
    });
});
The Lightning application is very simple:
 
<aura:application access="GLOBAL" extends="ltng:outApp">
    <c:PricebookSelector />
</aura:application>
The lightning component is as equally simple:
<aura:component controller="PricebookSelectorController">
    <aura:attribute name="pricebooks" type="List" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:component>
The component controller looks like this:
({
    doInit : function(component, event, helper) {
        helper.getPricebooks(component);
    }
})
Finally, the component helper looks like this:
({
    getPricebooks: function(component) {
        var action = component.get("c.getPricebooks");

	    var self = this;
	    action.setCallback(this, function(actionResult) {
		    console.log(actionResult);
	    });

	    $A.enqueueAction(action);
    }
})
The problem:

When I load up the VF page, I get a debug to the console that the component has finished loading (as expected). However, I get two separate, but identical, log messages with the action result. Why are two calls made to the server??

Thanks so much. =)




 
Best Answer chosen by Bobby Knezevic
James LoghryJames Loghry
Not sure if this will make a difference, but in your aura:application, try replacing:
 
<c:PricebookSelector />

with:
 
<aura:dependency resource="c:PricebookSelector"/>

 

All Answers

James LoghryJames Loghry
Not sure if this will make a difference, but in your aura:application, try replacing:
 
<c:PricebookSelector />

with:
 
<aura:dependency resource="c:PricebookSelector"/>

 
This was selected as the best answer
Bobby KnezevicBobby Knezevic
That totally worked! I suppose the $Lightning.use() method actually includes the applications content as well? Then when I load the component it runs it once more. Very interesting ....

Thanks for your help! =)
Max PaqMax Paq
This fixed my issue today! I have to ask, what exactly is the difference between using the component directly and using aura:dependency?
Shivam LalwaniShivam Lalwani
Thanks alot, it worked. But what 'aura:dependency' does exactly ?
Prashant Singh 214Prashant Singh 214
This is fixed my issue very easily.
labreports.upcovid19tracks.in (https://www.gorakhpurhindinews.com/online-labreports-upcovid19tracks-in/)