SlideShare a Scribd company logo
Mastering the
Lightning Framework
JF Paradis
Principal Engineer - Salesforce
@jfparadis
Part 2 – The programmatic aspects
Four sections
XML
CSS Apex
JS
Section 1:
XML Component Definition
Section 2:
Styling Components
Section 3:
JS Controller and Helper
Section 4:
Apex Controller
Section 3: JS Controller and Helper
3.1 MVC in Lightning
3.2 JS Controller
3.3 Events and Methods
3.4 Helper
3.1 MVC in Lightning
Model–View–Controller (MVC) is a software architectural pattern for
implementing user interfaces:
• The model, consists of application data, business rules, logic and functions.
It’s not only a database.
• The view can be any output representation of information, such as a chart or a
diagram.
• The controller, accepts input and converts it to commands for the model or
view.
Overview of MVC
MVC in single page web apps
Browser:
Backbone, Angular
Server:
Nancy, Spring
View
(HTML)
Controller
(JS)
Model
(JS)
View
(REST API)
Controller
(Java)
Model
(SQL)
MVC in Lightning
Browser Server
View
(HTML)
Controller
(JS)
Storage
(JS)
Controller
(Apex)
Model
(SQL)
Model: persistence
• Storage (topic covered in the Apex section)
• Component attributes v.something
View: presentation
• XML component definition
Controller: logic
• JS and Apex Controller
MVC in lightning
3.2 JS Controller
Example set 10:
JS Controller
/c/basics301.app
• JS file named <component>Controller.js
• Auto wired to the component
• Inherited when the component is extended
• Contains one JS object literal
• Members are functions called “actions”
• Actions parameter list is usually a tuple:
• cmp: the context, the current component
• event: if the action was called by an event
• helper: a pointer to a component file use for code
• Actions usually return nothing (undefined)
• No access to this (can’t call an action from an action)
JS Controller
Actions are similar to callbacks
Example set 11:
Anchor onclick vs ui:button press
/c/basics302.app
Actions can receive two types of events
• DOM event
• e.g. anchor onclick
• event.target: the caller
• event.currentTarget: the listener
• need to prevent default to indicate you have handled call
• Lightning event
• e.g. ui:button press of type ui:press
• event.source: the caller, e.g. the button component
• no need to prevent default to indicate you have handled call
• event.getParam(<parameter>)
• e.g. event.getParam("domEvent”) returns the original DOM event inside ui:press
Main purposes of controller action
Used as events handlers:
1. Interaction with a component
• e.g. respond to onclick
2. Change handler
• e.g. respond to a value change
3. Event responder
• e.g. component event, global event
3.3 Events and Methods
Example set 12:
Firing and handling events
/c/basics202.app
Lightning Events
• Declared as a bundle, like a component
• One XML file named <event>.evt
• Can declare attributes
• Two types:
• Application events: global broadcast, don’t use them (no scope)
• Component events: bubble like DOM events
• Caution (2016/06, will change):
• components events don’t bubble inside component body
• they jump to the components that declare the attributes
• If you can’t write a component that will be used as a wrapper to catch
events in another component (as you can do with DOM events).
Component Methods
• Public API for controller actions
• Declared in the component XML
• Can declare attributes
• Point to a controller action
• Parameters passed through the event attribute of the action
3.4 Helper
Helper
• JS file named <component>Helper.js
• Auto wired to the component.
• One instance per level of inheritance.
• Contains one JS object literal.
• Members are JS constants, JS functions, JS shared variables.
• No restrictions on function parameters and return types.
• Helpers are singletons: all components of a given type share the same
instance.
Controller ot Helper?
Controller tasks:
• “callbacks”
• get/set component attributes
• respond to events
• fire events (other components)
Helper tasks:
• code sharing/reuse between controller
actions
• reduce bloat on the controller
• process data
• fire events (server, other components)
• create testable functions
Section 4: Using Apex
4.1 Apex Controller
4.2 Action Service
4.1 Apex Controller
Apex Controller: definition
On the server side:
• Apex Class - Singleton
• Instance methods are “actions” annotated with @AuraEnabled
• Not part of the component bundle
• Return value auto serialized as JSON
On the client side:
• Wired using attribute controller="<class>"
• Accessed with cmp.get("{!c.<action>}")
• Response in action.getReturnValue()
• Return value is a JS object
Apex Controller: example
Example set 13:
Apex Controller
/c/basics401.app
4.2 Action Service
Lightning Action Service
• A framework to invoke client- and server-side logic
• Emphasis on performance
• Multiple actions are multiplexed in a single XHR
• Emphasis on security
• Constraints on target hosts via CSP
• Ensures component can only talk to its controller
• Mobile-centric caching
• Support offline
• Use cached value then async update cache + caller
Action control flow
Foreground
• Default
• Batched
• To reduce number of requests
Background
• Sent individually
• For long server-side execution
Storable
• Result cached
• To reduce repeated calls for the same data
Abortable
• Canceled if same action triggered again
before completion
• To avoid multiple results
Lightning event cycle
Lightning uses a stack to keep track of
the deferred tasks to process. At the end
of a run cycle, all queued actions are
executed.
Fire
Execute
handlers
Queue
actions
Execute
queued
actions
Example set 14:
Action Control Flow
/c/basics402.app
Action State
NEW: The action was created but is not in progress yet
RUNNING: The action is in progress
SUCCESS: The action executed successfully
ERROR: The server returned an error
INCOMPLETE: The server didn't return a response. The server might be down or
the client might be offline.
ABORTED: The action was aborted. You can register a callback for this explicitly.
• For server actions that are idempotent, replayable, and non-mutating
• Time-based expiration (15 min in SFX) auto-refresh (30s in SFX)
• Re-invoke callback only if refreshed value changes
Storable Lightning Actions
Storable Lightning Actions
Fire action
Action
callback
no
Action
callback
yes
Cache hit?
Add to
cache
Action
callback x2
no
yes
no
yes
Refresh?
Update
cache
Different?
Resources: Trailhead
Resources: Lightning section
Resources: Lightning section
thank y u
Mastering the Lightning Framework - Part 2

More Related Content

What's hot (20)

PDF
Angular 4 for Java Developers
Yakov Fain
 
PPT
Web controls
Sarthak Varshney
 
PPTX
Introduction to Angular JS
Santhosh Kumar Srinivasan
 
PDF
Integrating consumers IoT devices into Business Workflow
Yakov Fain
 
PPTX
Lightning Components Workshop
Gordon Bockus
 
PPTX
Angular 5
Bartłomiej Narożnik
 
PPTX
ASP.NET Page Life Cycle
Abhishek Sur
 
PDF
Get rid of controllers in angular 1.5.x start using component directives
Marios Fakiolas
 
PPTX
Angular 4 Introduction Tutorial
Scott Lee
 
ZIP
ASP.Net Presentation Part1
Neeraj Mathur
 
PDF
Apex Enterprise Patterns: Building Strong Foundations
Salesforce Developers
 
PPTX
Rits Brown Bag - TypeScript
Right IT Services
 
PPTX
Winter '19 release development.ppt
Kailas Shimpi
 
PDF
Building a custom Oracle ADF Component
Wilfred van der Deijl
 
PPTX
Ajax control asp.net
Sireesh K
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PPTX
Introduction to angular with a simple but complete project
Jadson Santos
 
PPTX
Angular JS, steal the idea
Scott Lee
 
PPTX
Mule Integration with Dropbox
Ramakrishna Narkedamilli
 
PPTX
Introduction Asp.Net Core, MVC, Docker (Linux), Postman and Swagger
Agusto Sipahutar
 
Angular 4 for Java Developers
Yakov Fain
 
Web controls
Sarthak Varshney
 
Introduction to Angular JS
Santhosh Kumar Srinivasan
 
Integrating consumers IoT devices into Business Workflow
Yakov Fain
 
Lightning Components Workshop
Gordon Bockus
 
ASP.NET Page Life Cycle
Abhishek Sur
 
Get rid of controllers in angular 1.5.x start using component directives
Marios Fakiolas
 
Angular 4 Introduction Tutorial
Scott Lee
 
ASP.Net Presentation Part1
Neeraj Mathur
 
Apex Enterprise Patterns: Building Strong Foundations
Salesforce Developers
 
Rits Brown Bag - TypeScript
Right IT Services
 
Winter '19 release development.ppt
Kailas Shimpi
 
Building a custom Oracle ADF Component
Wilfred van der Deijl
 
Ajax control asp.net
Sireesh K
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
Introduction to angular with a simple but complete project
Jadson Santos
 
Angular JS, steal the idea
Scott Lee
 
Mule Integration with Dropbox
Ramakrishna Narkedamilli
 
Introduction Asp.Net Core, MVC, Docker (Linux), Postman and Swagger
Agusto Sipahutar
 

Similar to Mastering the Lightning Framework - Part 2 (20)

PPTX
Mastering the Lightning Framework - Part 2 - JF Paradis.pptx
RhondaJolaoso
 
PDF
Lightning Components - Down Under Dreaming Sydney
Techforce Services
 
PPTX
#ImpactSalesforceSaturday: Lightning Components 101: An Apex Developer’s Guide
New Delhi Salesforce Developer Group
 
PPTX
Lightning Component - Components, Actions and Events
Durgesh Dhoot
 
PDF
No code to lighting component developer dreamforce 2016
Kerry Townsend
 
PPTX
Introduction to lightning lifecycle
SmritiSharan1
 
PDF
Lightning Components Explained
Atul Gupta(8X)
 
PPTX
Lightning components ver1.0
Akhil Anil ☁
 
PPTX
Denver Salesforce Developer User Group dec 2016 lightning components
Mike Tetlow
 
PPTX
10 commandments for writing spiffy Lightning Apps
Anup Jadhav
 
PDF
Easy REST Integrations with Lightning Components and Salesforce1
Salesforce Developers
 
PDF
All Aboard the Lightning Components Action Service
Peter Chittum
 
PPTX
Salesforce Lightning Web Components Overview
Nagarjuna Kaipu
 
PPTX
Salesforce Lightning workshop
Shivanath Devinarayanan
 
PPTX
Lightning Component Framework from 0 to App
Diego Szuster Marçal
 
PDF
LWC Episode 3- Component Communication and Aura Interoperability
Salesforce Developers
 
PPTX
Suisse Romande SF DG - Lightning workshop
Gnanasekaran Thoppae
 
PPTX
LWC_Workbxcgbgfbgfbfgbfgbfbfbshop_Day2.pptx
Vkrish Peru
 
PDF
Deep diving into building lightning components
Cloud Analogy
 
PPTX
Salesforce Lightning workshop Hartford - 12 March
Jitendra Zaa
 
Mastering the Lightning Framework - Part 2 - JF Paradis.pptx
RhondaJolaoso
 
Lightning Components - Down Under Dreaming Sydney
Techforce Services
 
#ImpactSalesforceSaturday: Lightning Components 101: An Apex Developer’s Guide
New Delhi Salesforce Developer Group
 
Lightning Component - Components, Actions and Events
Durgesh Dhoot
 
No code to lighting component developer dreamforce 2016
Kerry Townsend
 
Introduction to lightning lifecycle
SmritiSharan1
 
Lightning Components Explained
Atul Gupta(8X)
 
Lightning components ver1.0
Akhil Anil ☁
 
Denver Salesforce Developer User Group dec 2016 lightning components
Mike Tetlow
 
10 commandments for writing spiffy Lightning Apps
Anup Jadhav
 
Easy REST Integrations with Lightning Components and Salesforce1
Salesforce Developers
 
All Aboard the Lightning Components Action Service
Peter Chittum
 
Salesforce Lightning Web Components Overview
Nagarjuna Kaipu
 
Salesforce Lightning workshop
Shivanath Devinarayanan
 
Lightning Component Framework from 0 to App
Diego Szuster Marçal
 
LWC Episode 3- Component Communication and Aura Interoperability
Salesforce Developers
 
Suisse Romande SF DG - Lightning workshop
Gnanasekaran Thoppae
 
LWC_Workbxcgbgfbgfbfgbfgbfbfbshop_Day2.pptx
Vkrish Peru
 
Deep diving into building lightning components
Cloud Analogy
 
Salesforce Lightning workshop Hartford - 12 March
Jitendra Zaa
 
Ad

More from Salesforce Developers (20)

PDF
Sample Gallery: Reference Code and Best Practices for Salesforce Developers
Salesforce Developers
 
PDF
Maximizing Salesforce Lightning Experience and Lightning Component Performance
Salesforce Developers
 
PDF
Local development with Open Source Base Components
Salesforce Developers
 
PPTX
TrailheaDX India : Developer Highlights
Salesforce Developers
 
PDF
Why developers shouldn’t miss TrailheaDX India
Salesforce Developers
 
PPTX
CodeLive: Build Lightning Web Components faster with Local Development
Salesforce Developers
 
PPTX
CodeLive: Converting Aura Components to Lightning Web Components
Salesforce Developers
 
PPTX
Enterprise-grade UI with open source Lightning Web Components
Salesforce Developers
 
PPTX
TrailheaDX and Summer '19: Developer Highlights
Salesforce Developers
 
PDF
Live coding with LWC
Salesforce Developers
 
PDF
Lightning web components - Episode 4 : Security and Testing
Salesforce Developers
 
PDF
Lightning web components episode 2- work with salesforce data
Salesforce Developers
 
PDF
Lightning web components - Episode 1 - An Introduction
Salesforce Developers
 
PDF
Migrating CPQ to Advanced Calculator and JSQCP
Salesforce Developers
 
PDF
Scale with Large Data Volumes and Big Objects in Salesforce
Salesforce Developers
 
PDF
Replicate Salesforce Data in Real Time with Change Data Capture
Salesforce Developers
 
PDF
Modern Development with Salesforce DX
Salesforce Developers
 
PDF
Get Into Lightning Flow Development
Salesforce Developers
 
PDF
Integrate CMS Content Into Lightning Communities with CMS Connect
Salesforce Developers
 
PDF
Introduction to MuleSoft
Salesforce Developers
 
Sample Gallery: Reference Code and Best Practices for Salesforce Developers
Salesforce Developers
 
Maximizing Salesforce Lightning Experience and Lightning Component Performance
Salesforce Developers
 
Local development with Open Source Base Components
Salesforce Developers
 
TrailheaDX India : Developer Highlights
Salesforce Developers
 
Why developers shouldn’t miss TrailheaDX India
Salesforce Developers
 
CodeLive: Build Lightning Web Components faster with Local Development
Salesforce Developers
 
CodeLive: Converting Aura Components to Lightning Web Components
Salesforce Developers
 
Enterprise-grade UI with open source Lightning Web Components
Salesforce Developers
 
TrailheaDX and Summer '19: Developer Highlights
Salesforce Developers
 
Live coding with LWC
Salesforce Developers
 
Lightning web components - Episode 4 : Security and Testing
Salesforce Developers
 
Lightning web components episode 2- work with salesforce data
Salesforce Developers
 
Lightning web components - Episode 1 - An Introduction
Salesforce Developers
 
Migrating CPQ to Advanced Calculator and JSQCP
Salesforce Developers
 
Scale with Large Data Volumes and Big Objects in Salesforce
Salesforce Developers
 
Replicate Salesforce Data in Real Time with Change Data Capture
Salesforce Developers
 
Modern Development with Salesforce DX
Salesforce Developers
 
Get Into Lightning Flow Development
Salesforce Developers
 
Integrate CMS Content Into Lightning Communities with CMS Connect
Salesforce Developers
 
Introduction to MuleSoft
Salesforce Developers
 
Ad

Recently uploaded (20)

PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Executive Business Intelligence Dashboards
vandeslie24
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 

Mastering the Lightning Framework - Part 2

  • 1. Mastering the Lightning Framework JF Paradis Principal Engineer - Salesforce @jfparadis Part 2 – The programmatic aspects
  • 2. Four sections XML CSS Apex JS Section 1: XML Component Definition Section 2: Styling Components Section 3: JS Controller and Helper Section 4: Apex Controller
  • 3. Section 3: JS Controller and Helper 3.1 MVC in Lightning 3.2 JS Controller 3.3 Events and Methods 3.4 Helper
  • 4. 3.1 MVC in Lightning
  • 5. Model–View–Controller (MVC) is a software architectural pattern for implementing user interfaces: • The model, consists of application data, business rules, logic and functions. It’s not only a database. • The view can be any output representation of information, such as a chart or a diagram. • The controller, accepts input and converts it to commands for the model or view. Overview of MVC
  • 6. MVC in single page web apps Browser: Backbone, Angular Server: Nancy, Spring View (HTML) Controller (JS) Model (JS) View (REST API) Controller (Java) Model (SQL)
  • 7. MVC in Lightning Browser Server View (HTML) Controller (JS) Storage (JS) Controller (Apex) Model (SQL)
  • 8. Model: persistence • Storage (topic covered in the Apex section) • Component attributes v.something View: presentation • XML component definition Controller: logic • JS and Apex Controller MVC in lightning
  • 10. Example set 10: JS Controller /c/basics301.app
  • 11. • JS file named <component>Controller.js • Auto wired to the component • Inherited when the component is extended • Contains one JS object literal • Members are functions called “actions” • Actions parameter list is usually a tuple: • cmp: the context, the current component • event: if the action was called by an event • helper: a pointer to a component file use for code • Actions usually return nothing (undefined) • No access to this (can’t call an action from an action) JS Controller
  • 12. Actions are similar to callbacks
  • 13. Example set 11: Anchor onclick vs ui:button press /c/basics302.app
  • 14. Actions can receive two types of events • DOM event • e.g. anchor onclick • event.target: the caller • event.currentTarget: the listener • need to prevent default to indicate you have handled call • Lightning event • e.g. ui:button press of type ui:press • event.source: the caller, e.g. the button component • no need to prevent default to indicate you have handled call • event.getParam(<parameter>) • e.g. event.getParam("domEvent”) returns the original DOM event inside ui:press
  • 15. Main purposes of controller action Used as events handlers: 1. Interaction with a component • e.g. respond to onclick 2. Change handler • e.g. respond to a value change 3. Event responder • e.g. component event, global event
  • 16. 3.3 Events and Methods
  • 17. Example set 12: Firing and handling events /c/basics202.app
  • 18. Lightning Events • Declared as a bundle, like a component • One XML file named <event>.evt • Can declare attributes • Two types: • Application events: global broadcast, don’t use them (no scope) • Component events: bubble like DOM events • Caution (2016/06, will change): • components events don’t bubble inside component body • they jump to the components that declare the attributes • If you can’t write a component that will be used as a wrapper to catch events in another component (as you can do with DOM events).
  • 19. Component Methods • Public API for controller actions • Declared in the component XML • Can declare attributes • Point to a controller action • Parameters passed through the event attribute of the action
  • 21. Helper • JS file named <component>Helper.js • Auto wired to the component. • One instance per level of inheritance. • Contains one JS object literal. • Members are JS constants, JS functions, JS shared variables. • No restrictions on function parameters and return types. • Helpers are singletons: all components of a given type share the same instance.
  • 22. Controller ot Helper? Controller tasks: • “callbacks” • get/set component attributes • respond to events • fire events (other components) Helper tasks: • code sharing/reuse between controller actions • reduce bloat on the controller • process data • fire events (server, other components) • create testable functions
  • 23. Section 4: Using Apex 4.1 Apex Controller 4.2 Action Service
  • 25. Apex Controller: definition On the server side: • Apex Class - Singleton • Instance methods are “actions” annotated with @AuraEnabled • Not part of the component bundle • Return value auto serialized as JSON On the client side: • Wired using attribute controller="<class>" • Accessed with cmp.get("{!c.<action>}") • Response in action.getReturnValue() • Return value is a JS object
  • 27. Example set 13: Apex Controller /c/basics401.app
  • 29. Lightning Action Service • A framework to invoke client- and server-side logic • Emphasis on performance • Multiple actions are multiplexed in a single XHR • Emphasis on security • Constraints on target hosts via CSP • Ensures component can only talk to its controller • Mobile-centric caching • Support offline • Use cached value then async update cache + caller
  • 30. Action control flow Foreground • Default • Batched • To reduce number of requests Background • Sent individually • For long server-side execution Storable • Result cached • To reduce repeated calls for the same data Abortable • Canceled if same action triggered again before completion • To avoid multiple results
  • 31. Lightning event cycle Lightning uses a stack to keep track of the deferred tasks to process. At the end of a run cycle, all queued actions are executed. Fire Execute handlers Queue actions Execute queued actions
  • 32. Example set 14: Action Control Flow /c/basics402.app
  • 33. Action State NEW: The action was created but is not in progress yet RUNNING: The action is in progress SUCCESS: The action executed successfully ERROR: The server returned an error INCOMPLETE: The server didn't return a response. The server might be down or the client might be offline. ABORTED: The action was aborted. You can register a callback for this explicitly.
  • 34. • For server actions that are idempotent, replayable, and non-mutating • Time-based expiration (15 min in SFX) auto-refresh (30s in SFX) • Re-invoke callback only if refreshed value changes Storable Lightning Actions
  • 35. Storable Lightning Actions Fire action Action callback no Action callback yes Cache hit? Add to cache Action callback x2 no yes no yes Refresh? Update cache Different?