Wednesday, February 21, 2018

Load JS and CSS in Lightning Component from Static Resource

Suppose we have a ZIP file containing CSS and JS files, available under Static Resources (MIME Type : application/zip) and we want to load it in a lightning component, then check the code synapse needs to be update under .cmp file for solution ,

sample.zip - | sampleCss.css
                     | sampleJs.js


<aura:component>
 <ltng:require styles="{!$Resource.sample + '/sampleCss.css'}" 
                  scripts="{!$Resource.sample + '/sampleJs.js'}" 
                  afterScriptsLoaded="{!c.afterScriptsLoadedMethodInController}"/>
<aura:component>

Tuesday, February 20, 2018

Lightning UI Date Time Format


<lightning:formattedDateTime aura:id="thresholdRunOn"
                             value="your date time value"
                             month="short"
                             day="numeric"
                             year="numeric"
                             hour="2-digit"
                             minute="2-digit"
                             second="2-digit"
                             hour12="true"
                             timeZone="{!$Locale.timezone}"/>

Output:
The above example will return a value like this ==> Feb 20, 2018, 9:43:00 PM

Note: Properties value can be change based on your requirement.

Saturday, January 6, 2018

aura:method example in Lightning

Scenario: Scenario is same as posted in my previous post (Lightning Event Example) and the aim is also same and that is Pass value from child component to parent component.

Solution: 
To execute the above scenario, we can also use aura:method instead of aura:event.
For the same we will create a aura:method under child component and this method will return a string value and from parent component we will catch this value.

1. Create a Child component (childComponent.cmp). Under this child component declare a aura:method as shown in below example. aura:method has a name (this name will be used called from parent component), an action (for our example, this action method will return a string value) and set access as "PUBLIC".

childComponent.cmp
<!-- Child component with aura method -->
<aura:component >
    <aura:method name="messageFromChild" 
                 action="{!c.getMessageFromChild}" 
                 access="PUBLIC"/>
</aura:component>

childComponentController.js

//js controller for child component
({
 getMessageFromChild : function(component, event, helper) {
  return "##### I am under CHILD component. #####";
 }
})

2. Now create a parent component (parentComponent.cmp) and from parent component call the child component.
 Parent component have an local attribute (messageFromChild), with default value as "##### I am under PARENT component. #####" and a button (click on this button will call an action method, from where child method will be called and return value from child method will be set in local attribute.).
So, when this parent component will execute in UI, first it will show a button with an output text message as "##### I am under PARENT component. #####" and when the button will clicked then that output text message will display as "##### I am under CHILD component. #####".

parentComponent.cmp

<!-- Parent component to call child component to execute aura method -->
<aura:component implements="force:lightningQuickAction">
    <!-- Local attribute whith default value -->
    <aura:attribute name="messageFromChild" 
                    type="String" 
                    default="##### I am under PARENT component. #####"/>
    
    <!-- Child component calling. This should have aura:id -->
    <c:childComponent aura:id="childComp"/>
    
    <!-- On button click child aura:method will be called. -->
    <ui:button label="Call Child Method" press="{!c.callChildMethod}" />
    
    <!-- Initially display default message set in this parent componet.
   Once button click this attribute will be set by a string 
   value comming from child component. -->
    <p>{!v.messageFromChild}</p>
</aura:component>

parentComponentController.js

//js controller for parent component
({
 callChildMethod : function(component, event, helper) {
        //Child aura method calling logic.
        var childCmp = component.find("childComp");
        var rtnMsgFromChildMethod = childCmp.messageFromChild();
        
        //Set local attribute with string value, comming from child aura method.
        component.set("v.messageFromChild", rtnMsgFromChildMethod);
 }
})


Output on initial load (before click on button):


Output after button click:


Friday, December 29, 2017

Lightning Event Example

Scenario: [Component Event]
1. Create 2 components. One child (childComponent.cmp) and one parent (parentComponent.cmp).
2. Call child component from parent component.
3. Parent component has a String attribute with default value as "Default message.", which will be displayed once this component load first time.
4. Child component has a button.
5. So, once parent component will call then in UI, we will see "one button" (part of child component) and "one string message" (part of parent component).
6. Now the challenge is that, click on the button will set the "Default message." as "Message passing from child component."
In one word, our aim is to pass value from child component to parent component.


Solution:
To execute the above scenario, we will pay with Lightning Event mechanism.

1. So first create an event which will store an event message passing from child component and execute it in parent component.

messagePassingEvent.evt
<!-- messagePassingEvent.evt -->
<aura:event type="COMPONENT" description="Responsible for passing message from child to parent">
    <aura:attribute name="message" type="String"/>
</aura:event>

2. Now create a child component (childComponent.cmp) as show in below source code. Child component should has a button and click on that button will call an action method.
Now in child component registered the above created event (messagePassingEvent.evt) and under action method (in controller) set the event message and then fire it, so that from parent component this message will be fetched and display in UI.

childComponent.cmp
<!-- childComponent.cmp -->
<aura:component>
    <!-- Register event -->
    <aura:registerEvent name="messageEvent" type="c:messagePassingEvent"/>
    
    <!-- Fire event on button click -->
    <ui:button label="Fire Component Event" press="{!c.fireComponentEvent}" />
</aura:component>

childComponentController.js
//childComponentController.js
({
 fireComponentEvent : function(component, event, helper) {
     //Find event
            var msgFrmEvent = component.getEvent("messageEvent");
        
            //Set event parameter
            msgFrmEvent.setParams({"message": "Message passing from child component."});  
        
            //Fire event
            msgFrmEvent.fire();
 }
})

3. Now create a parent component (parentComponent.cmp) as show in below source code. From parent component will call child component and also parent component should has a string attribute with default message, which will be replaced with event message passed from child component by event firing.
So, in parent component we will create an event handler method, from where we will set the event message need to be displayed once button will be clicked.

parentComponent.cmp
<!-- parentComponent.cmp -->
<aura:component implements="force:lightningQuickAction">
    <!-- Local attribute -->
    <aura:attribute name="messageFromEvent" 
                    type="String" 
                    default="Default message."/>    
    
    <!-- calling event handling action method. "name" attribute should be same
   as mentioned on event registration in child component -->
    <aura:handler name="messageEvent" 
                  event="c:messagePassingEvent" 
                  action="{!c.checkChildMessage}"/>
    
    <!-- Calling child component -->
    <c:childComponent />
    
     <p>{!v.messageFromEvent}</p>
</aura:component>

parentComponentController.js
//parentComponentController.js
({
 checkChildMessage : function(component, event, helper) {
        //Fetch event message set in child component.
        var eventMsgFrmChild  = event.getParam("message");
        
        //Set event message to display in UI.
        component.set("v.messageFromEvent", eventMsgFrmChild);
 }
})


4. Result screen while executing [parentComponent]

















5. Result screen while click on button.
















Application Event Syntax :

Event  (ApplicationEvntExample.evt):
event type should be set as APPLICATION.
<aura:event type="APPLICATION" description="application event">
    <aura:attribute name="" type=""/>

</aura:event>


Firing component :
1. Event registration under .cmp file
        <aura:registerEvent name="applicationEvntExample" type="c:ApplicationEvntExample"/>

2. Firing event from controller/helper method
     
        var eventInst = $A.get("e.c:ApplicationEvntExample");
        eventInst.setParams({
               //set your parameters here as mentioned below
               //"param name"   : param value,
              //"param name"   : param value,             
        });
        eventInst.fire();
 
Receiving component :
1. Under .cmp file
     <aura:handler name="applicationEvntExm"
                  event="c:ApplicationEvntExample"
                  action="{!c.sampleMethod}"/>

2. Under Controller
    sampleMethod : function(component, event, helper) {
           event.getParam("param name");
}

Tuesday, December 19, 2017

Client side log message for lightning component.

Why to use?

1. Suppose we want to check any log message in our client side controller or helper method.
2. Suppose we want to check a client side controller or helper method is calling from a button click or not.
3. Suppose we want to check user request parameter(s) is coming properly or not to a client side controller or helper method.
4. Suppose we want to check server-side response.


How to develop?

In your controller or helper method use the below line of code,

console.log("Your condole log message.");


How to check?

Suppose we have a "Save" button in our lightning component and click on "Save" button will call a controller "doSave()" method.
Now we want to check is this method is calling or not? So, create a console.log("I am under doSave method.") under this doSave() method. Now open this component in browser and also open console window [For eg. to open console window in google chrome use "Ctrl + Shift + i "].
Now click on "Save" button and check console window and if every thing is OK, then we will see log message as "I am under doSave method." in console window.

Shortcut key for Lightning Component Button.

Scenario: Use the previously posted lightning component (UpdateStudent) [Post Subject : Crate a lightning component to update record.].
Now suppose we want to create/develop shortcut keys for "Save" [Alt + s] and "Cancel" [Alt + c] button for out component.


Solution: To develop the shortcut key in lightning component use "accesskey" key-word under your "lightning:button" component. So, for the above scenario use the below syntax under your "lightning:button" component,

  • For Save : accesskey="s"
  • For Cancel : accesskey="c"
And the button code base is look like this for our UpdateStudent component,

                <lightning:button aura:id="cancelId"
                                  label="Cancel"
                                  variant="brand"
                                  onclick="{!c.doCancel}"
                                  accesskey="c"/>
               
                <lightning:button aura:id="saveId"
                                  label="Save"
                                  variant="neutral"
                                  onclick="{!c.doSave}"
                                  accesskey="s"/>

So, update the component's buttons part with above code base and use "Alt + s" and "Alt + c" for save and cancel operation respectively.

How to add "lightning spinner" in a lightning component.

Please check my previous post "Crate a lightning component to update record." This post is an example of open a modal to update student detail.
Now, here we want to display a "spinner", when we click on "Save" button. This spinner will be displayed until the back end server side operation will be in process.

To perform the above scenario, fist execute the previous post (Crate a lightning component to update record.) and then add the below line of code in component(UpdateStudent.cmp) and controller(UpdateStudent.Controller.js).

Delta modification on component:

1. Add the below attribute :

<aura:attribute name="Spinner" type="Boolean" default="false" access="global"/>

2. Add below 2 aura:handlers :

<aura:handler event="aura:waiting" action="{!c.showSpinner}"/>
<aura:handler event="aura:doneWaiting" action="{!c.hideSpinner}"/>

3. Now add the below condition under modal body :
<aura:if isTrue="{!v.Spinner}">
      <lightning:spinner variant="brand" size="large"/>
 </aura:if>

Delta modification on controller:

Now add the below 2 methods under controller (UpdateStudent.Controller.js),

/**
     * @name:    showSpinner      
     * @date:        19 DEC 2017
     * @description: To show the Spinner when anything is in progress status from Server side.
    **/
    showSpinner: function(component, event, helper) {
       component.set("v.Spinner", true);
    },
    
    /**
     * @name:    hideSpinner      
     * @date:        19 DEC 2017
     * @description: To hide the Spinner when all Server side action has been coplelted.
    **/
    hideSpinner : function(component,event,helper){
     component.set("v.Spinner", false);
    },


After successful completion of above update, we can test out spinner functionality. For the same if we click on save button or on the time of initial record value load (edit button click) we will see a spinner as a processing activity.


Data Cloud Part 6: Practical (Ingest Physical Store's Customer details and Sales details)

Hope you have already created your Data Cloud Org and also downloaded the Customer's details and Sales details from previous posts.  Now...