10 commandments for writing spiffy Lightning Apps

41
10 Commandments for writing spiffy Lightning Apps Anup Jadhav Technical Architect & Force.com MVP [email protected] @anup

Transcript of 10 commandments for writing spiffy Lightning Apps

Page 1: 10 commandments for writing spiffy Lightning Apps

10 Commandments for writing spiffy Lightning Apps

Anup Jadhav Technical Architect & Force.com MVP [email protected] @anup

Page 2: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 1

LEARN JAVASCRIPT

Page 3: 10 commandments for writing spiffy Lightning Apps

LEARN JAVASCRIPT DO YOU GROK THESE CONCEPTS?

Immediately Invoked Function Expression ( IIFE pronounced iffy )

ClosuresPrototypal InheritanceVariable HoistingPromises

Page 4: 10 commandments for writing spiffy Lightning Apps

LEARN JAVASCRIPT START WITH BASICS

RESOURCESMozilla Developer Network (MDN) javascript tutorial

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)

Eloquent Javascript, Second Edition (http://eloquentjavascript.net/)

You Don’t Know JS by Kyle Simpson (https://github.com/getify/You-Dont-Know-JS)

Read JavaScript code written by front-end developers

Page 5: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 2

FORGET MVC

Page 6: 10 commandments for writing spiffy Lightning Apps

FORGET MVC

Lightning framework follows a variation of MVCThink in terms of View-Controller-Controller-Model

(quite similar to MVVM pattern)Lightning Component / App Bundle

Client-side component (View)Client side JavaScript Controller (& helper)Server side Apex ControllerServer side Model (sObjects)

Page 7: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 3

SUPPORT ACCESSIBILITY

Page 8: 10 commandments for writing spiffy Lightning Apps

SUPPORT ACCESSIBILITY

Lightning Components created according to W3C spec

follows the Web Content Accessibility Guidelines (WCAG)

Page 9: 10 commandments for writing spiffy Lightning Apps

SUPPORT ACCESSIBILITY

Some tips to make your component accessible:Set the label attribute when its available

ui:buttonui:menuui:inputTextui:inputDate

TIPS

Page 10: 10 commandments for writing spiffy Lightning Apps

SUPPORT ACCESSIBILITY

Use the ariaDescribedby attribute to associate the help text or error message with a particular field

TIPS

Page 11: 10 commandments for writing spiffy Lightning Apps

SUPPORT ACCESSIBILITY

Some tips to make your component accessible (continued…) Convey audio notifications using ui:message

component

Consider attaching onclick event to elements that are actionable in HTML by default <a>, <button>, or <input>

Page 12: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 4

UNDERSTAND EVENT PROPOGATION

Page 13: 10 commandments for writing spiffy Lightning Apps

EVENT PROPOGATION

Component EventsFired from an instance of the

componentHandled by component itself

or by component in the “containment” hierarchy

Supports capture & bubble (default) phase

Application EventsFollow a traditional publish-

subscribe modelAll components that provide a

handler are notifiedSupports capture, bubble,

and default phase

Page 14: 10 commandments for writing spiffy Lightning Apps

EVENT PROPOGATION

BEST PRACTICE

Always (try) to use component event instead of an Application event

Don’t fire an event in a Renderer (anti-pattern) Separate low-level events from business logic

events Use a dispatcher component to listen and relay

events

Page 15: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 5

PREFER SLDS OVER OTHER CSS FRAMEWORKS

Page 16: 10 commandments for writing spiffy Lightning Apps

SALESFORCE LIGHTNING DESIGN SYSTEM

SLDS FRAMEWORK is composed of: semantic and accessible component markup cross-browser compatible CSS icons, fonts, and (Lightning UX) design principles

WHAT IS IT?

Page 17: 10 commandments for writing spiffy Lightning Apps

SALESFORCE LIGHTNING DESIGN SYSTEM

Matches Lightning Experience (LEx) UI Follows modern design standards and best practice Actively maintained by Salesforce UX team Styles your component to be more accessible Makes your component & app responsive

WHY WOULD YOU WANT TO USE IT?

Page 18: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 6

USE BASE LIGHTNING COMPONENTS

Page 19: 10 commandments for writing spiffy Lightning Apps

BASE LIGHTNING COMPONENTS

Introduced in Winter ‘17 release Incorporates SLDS markup and classes Handles the details of HTML & CSS

Button

Select

Tabs

Input

Rich Text Area

Text Area

Avatar

Badge

Spinner

Icon

Page 20: 10 commandments for writing spiffy Lightning Apps

BASE LIGHTNING COMPONENTS

Easy to adopt, patterned after HTML standards

Styled using SLDS (minimal CSS) Better performance Supports Accessibility (WCAG 2.0 & Section

508 standards) Streamlines Lightning Development

WHY WOULD YOU WANT TO USE IT?

Page 21: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 7

SECURE YOUR COMPONENTS

Page 22: 10 commandments for writing spiffy Lightning Apps

SECURE YOUR COMPONENTS

Components don’t automatically enforce CRUD / FLS Manually enforce CRUD & FLS in your apex controller

FLS / CRUD check

Page 23: 10 commandments for writing spiffy Lightning Apps

SECURE YOUR COMPONENTS

CHECK FLS/CRUD in Apex

Page 24: 10 commandments for writing spiffy Lightning Apps

SECURE YOUR COMPONENTS

Content Security Policy – to control the source of content that can be loaded on a page All JS libraries should be uploaded as static

resources All external fonts, images, frames & CSS must use

HTTPS CSP isn’t enforced by all browsers (caniuse.com)

Content Security Policy

Page 25: 10 commandments for writing spiffy Lightning Apps

SECURE YOUR COMPONENTS

Locker Service is your friend Tightens CSP to eliminate the possibility of XSS attacks Disallows the unsafe-inline and unsafe-eval keywords for

inline scripts Enforces EcmaScript 5 strict mode DOM access Containment Restriction to Global references Access to supported JavaScript API framework methods

only Activated for all orgs in the Summer ’17 release

LOCKER SERVICE

Page 26: 10 commandments for writing spiffy Lightning Apps

SECURE YOUR COMPONENTS

Use Salesforce Lightning CLI to validate your code for use within the Locker Service Architecture

Install CLI

LIGHTNING CLI

Page 27: 10 commandments for writing spiffy Lightning Apps

SECURE YOUR COMPONENTS LIGHTNING CLI

Run CLI:

Page 28: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 8

AVOID PERFORMANCE BOTTLENECKS

Page 29: 10 commandments for writing spiffy Lightning Apps

LIGHTNING COMPONENT PERFORMANCE

Don’t use <aura:if> to toggle markup visibility hurts performance by creating a large

number of components leads to cluttered markup that is harder to

maintain Use CSS instead

TIPS

Page 30: 10 commandments for writing spiffy Lightning Apps

LIGHTNING COMPONENT PERFORMANCE

Review the warning messages logged by the framework

TIPS

Page 31: 10 commandments for writing spiffy Lightning Apps

LIGHTNING COMPONENT PERFORMANCE

Use Unbound expression instead of Bound expression

Bound Expression – {!expression}

UnBound Expression – {#expression}

TIPS

Page 32: 10 commandments for writing spiffy Lightning Apps

LIGHTNING COMPONENT PERFORMANCE

Avoid looping in the markup (if possible) Loop in the JavaScript or Apex controller

Use AppCache to speed up response time disabled by default set useAppCache=“true” attribute in the aura:application

tag cache manifest is auto-generated at runtime switch it off during dev

TIPS

Page 33: 10 commandments for writing spiffy Lightning Apps

LIGHTNING COMPONENT PERFORMANCE

Install the Salesforce Lightning Inspector (chrome) extension

Review the Performance Tab in the Lightning Inspector (chrome extension)

TIPS

Page 34: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 9

ERROR HANDLING

Page 35: 10 commandments for writing spiffy Lightning Apps

ERROR HANDLING

Easy to write erroneous code in JavaScript Type errors and typos that fail silently System errors due to insufficient input validation

Best practice Use try-catch in your controller and helper Validate your input Show user friendly error messages Log actual errors in browser dev console

Page 36: 10 commandments for writing spiffy Lightning Apps

ERROR HANDLING

TRY - CATCH BEST PRACTICE

Page 37: 10 commandments for writing spiffy Lightning Apps

ERROR HANDLING

VALIDATE YOUR INPUT BEST PRACTICE

Page 38: 10 commandments for writing spiffy Lightning Apps

COMMANDMENT 10

UNIT TEST JAVASCRIPT CODE

Page 39: 10 commandments for writing spiffy Lightning Apps

TESTING JAVASCRIPT CODE

Platform enforces Apex code coverage No requirement to unit test JS code to deploy

Why unit test JavaScript ? 60% plus code will be in JS Improves code quality Catch bugs early during Dev Ensure correctness

Page 40: 10 commandments for writing spiffy Lightning Apps

TESTING JAVASCRIPT CODE

How do I test Lightning Components? Salesforce has plans to release a testing framework

(safe-harbour) Greased tool – by Steve Buikhuizen

basically a Selenium test written in Lightning flavored JavaScript.

the test scripts are built using chains of Promises. End to end testing for Lightning Components

Use JS test framework like Jasmine or Q-unit (talk by @Bob_Buzzard)

Page 41: 10 commandments for writing spiffy Lightning Apps

Thank You

Anup Jadhav Technical Architect / Force.com MVP

[email protected] @anup

Remember to tell us what you think in the event survey www.LondonsCalling.net/survey/