Lightning chess

35
Code your own Lightning Components Including Lightning Chess Amsterdam World Tour - April 14th 2016

Transcript of Lightning chess

Page 1: Lightning chess

Code your own Lightning ComponentsIncluding Lightning Chess

Amsterdam World Tour - April 14th 2016

Page 2: Lightning chess

Lieven Juwet• Developer @ ABSI

• @LievenJuwet

Page 3: Lightning chess

Samuel De Rycke• Technical Solution Architect @ ABSI

• Salesforce MVP

• Co-Leader BE User Group

• @SamuelDeRycke

Page 4: Lightning chess

#SalesforceDevs

#SalesforceTour

#Lightning

Page 5: Lightning chess

Agenda

• Chess demo

• Lightning component framework

• Lightning components

• Communication between components

• Calling Apex

Page 6: Lightning chess

Demo: Lightning Chess

Page 7: Lightning chess

Lightning Component Framework

• Javascript/HTML5/CSS3 client

• Databinding & UI Rendering

• Stateful client & Stateless server

• Improved performance

• Component Encapsulation

• Event-driven approach

Page 8: Lightning chess

Lightning Component Framework

● Lightning Application

● Lightning Component

● Lightning Event

Page 9: Lightning chess

Lightning Component

• A Component bundle can contain multiple elements

• Component: UI Markup

• Controller

• Helper

• Style

• Renderer

Component Bundle

With each component comes its own responsibility!

Component Controller HelperStyle

RendererApex

Controller

Page 10: Lightning chess

Chess Components• Main_Game_Component

• Manage any type of game.

• Chessboard_Component• Manage the state of the chessboard by handling location and streaming events + chess logic

• Chessboard_Location_Component• Handle action events and render the correct state of his location.

• Player_List_Component• Manage and display the online players and issue challenges.

• Challenge_List_Component• Manage incoming challenges

• Streaming_API_Component• Subscribe to PushTopics and convert streaming events into lightning events

Page 11: Lightning chess

Lightning ComponentChessboard Component markup

<!-- Chessboard Component --><aura:component controller='ChessGameController' extends="c:Abstract_Game" > <!-- Set Attributes --> <aura:attribute type="Array" name="locations"></aura:attribute>

<!-- Set Event Listeners --> <aura:handler event="c:StreamingEvent" action="{!c.handleStreamingEvent}"/> <aura:handler event="c:Promotion_Complete" action="{!c.handlePromotion}"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

<!-- Set Event thrown --> <aura:registerEvent name="LocationAction" type="c:LocationAction"/> <aura:iteration var="row" items="{!v.locations}"> <aura:iteration var="location" items="{!row}"> <c:ChessBoardLocation location="{!location}" click="{!c.handleLocationClick}" />

</aura:iteration> </aura:iteration> </aura:component>

Page 12: Lightning chess

Lightning Component

• Declare the attribute components

• Attribute values are stored on the value provider (v)

• Access and set the values in Javascript

• Set by parent components

• Define access: Global, Public, Private

• Use them to bind your data to HTML/Components

Component Attributes

<aura:attribute name=”locations” type=”Array”></aura:attribute>

<aura:iteration var="row" items="{!v.locations}">

Page 13: Lightning chess

Lightning Component

• Locations updated by issuing component.set(‘v.locations’,newLocations)

• Re-render both iterations

• 64 new components created each time

• Move render responsibility from a central point for many components (heavy) to components individually.

Component rendering/re-rendering

<aura:iteration var="row" items="{!v.locations}"> <aura:iteration var="location" items="{!row}"> <c:ChessBoardLocation location="{!location}" click="{!c.handleLocationClick}" /> </aura:iteration> </aura:iteration>

Page 14: Lightning chess

Lightning Chess: Event driven approach

<!-- Chessboard Component --><aura:component controller='ChessGameController' extends="c:Abstract_Game" > ... <!-- Set Event Listeners --> <aura:handler event="c:StreamingEvent" action="{!c.handleStreamingEvent}"/> <aura:handler event="c:Promotion_Complete" action="{!c.handlePromotion}"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

<!-- Set Event thrown --> <aura:registerEvent name="LocationAction" type="c:LocationAction"/>

... </aura:component>

Page 15: Lightning chess

Lightning Chess: Component Communication

Chessboard

Chessboard Location

x64Location Clicked

Location Action

Chess Logic

Page 16: Lightning chess

Event driven approach: Benefits

• Each component has its own responsibility

• Loose coupling• Chessboard does not know who receives the event and what it does.• Location doesn’t know the subscriber and doesn’t know what happens

• Subscribe more components to these events• Example: Overview of fallen pieces

• Reusability of components• Example: History of chess games

Page 17: Lightning chess

Component Controller

Component Controller

• Contains Javascript functions

• Bind controller functions in the component markup

• Access component attributes in the controller

Page 18: Lightning chess

Component Controller

• Contains the Javascript functions

• Bind controller functions in the component markup

• Access component attributes in the controller

<div … onclick=”{!c.handleLocationClick}”>…

</div>

Page 19: Lightning chess

Component Controller

• Obtain and update component attribute values

• Send out new events

• Make a server call

handleLocationClick : function(cmp,event,helper){

var location = cmp.get(‘v.location’);

var e = cmp.getEvent(‘click’);e.setParams({‘location’ : location});e.fire();

}

Page 20: Lightning chess

Communication with Events

2 types of events

• Component event• Parent component can register a handler function directly• Send upwards in the component hierarchy through event bubbling

• Application Event• Functions as a broadcast.• All components in the application can register a handler

Page 21: Lightning chess

Component Event<aura:event type="COMPONENT">

<aura:attribute name="location" type="Object" />

</aura:event>

<aura:registerEvent name="click" type="c:ChessboardLocationClicked"/>

var e = cmp.getEvent('click'); e.setParams({'location':location}); e.fire();

<aura:handler name="click" event="c:ChessboardLocationClicked" action="{!c.handleLocationClick}"/>

<c:ChessBoardLocation location="{!location}" click="{!c.handleLocationClick}" />

Page 22: Lightning chess

Application Event <aura:event type="APPLICATION">

<aura:attribute name="payload" type="Object"/>

<aura:attribute name="actionType" type="String"/>

</aura:event>

<aura:registerEvent name="LocationAction" type="c:LocationAction"/>

var e = $A.get('e.c:LocationAction');

e.setParams({'payload':{'locations':possibleLocations},'actionType':'setSelectable'});

e.fire();

<aura:handler event="c:LocationAction" action="{!c.handleActionEvent}"/>

Page 23: Lightning chess

Component Helper: sharing functionality• Each component can have helper functions

• Contains functions that are used on multiple locations in the controller

• Avoid duplicate code!

Component Controller Helper

Page 24: Lightning chess

Component style

• Component CSS is encapsulated

Component Controller HelperStyle

Page 25: Lightning chess

Component style

Page 26: Lightning chess

Component renderer

Perform your DOM manipulations in the render/re-render functions

Component Controller HelperStyle

Renderer

Page 27: Lightning chess

Component renderer

Page 28: Lightning chess

Calling Apex

Component Controller HelperStyle

Renderer Apex Controller

Page 29: Lightning chess

Calling Apex

Use AuraEnabled methods retrieve/send data to the server

Link the APEX controller to the component

public class ChessboardController{

@AuraEnabledpublic static Object getBoardPieces(Id game) {

return [select Active__c,Piece_Color__c, … from ChessPiece__c where Chessboard__c = :game];

}}

<aura:component controller=”ChessboardController”> … </aura:component>

Page 30: Lightning chess

Calling Apex

var action = cmp.get(‘c.getBoardPieces’);action.setParams( {‘game’:chessboard.Id});action.setCallback(this,function(response){

if(!cmp.isValid())return

if(response.getState() == ‘SUCCESS’){

//Handle the response}

}

$A.enqueueAction(action);if

Page 31: Lightning chess

Overview

• Chess demo

• Lightning component framework

• Lightning components

• Communication between components

• Calling Apex

Page 32: Lightning chess
Page 33: Lightning chess

Go to Developer User Groups!

Belgium: http://bit.ly/1Xqlu5p

The Netherlands: http://bit.ly/1Sbdzuo

Page 34: Lightning chess

thank y uQuestions?

Page 35: Lightning chess

thank y u