Lightning chess
-
Upload
lieven-juwet -
Category
Technology
-
view
682 -
download
0
Transcript of Lightning chess
![Page 1: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/1.jpg)
Code your own Lightning ComponentsIncluding Lightning Chess
Amsterdam World Tour - April 14th 2016
![Page 2: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/2.jpg)
Lieven Juwet• Developer @ ABSI
• @LievenJuwet
![Page 3: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/3.jpg)
Samuel De Rycke• Technical Solution Architect @ ABSI
• Salesforce MVP
• Co-Leader BE User Group
• @SamuelDeRycke
![Page 4: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/4.jpg)
#SalesforceDevs
#SalesforceTour
#Lightning
![Page 5: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/5.jpg)
Agenda
• Chess demo
• Lightning component framework
• Lightning components
• Communication between components
• Calling Apex
![Page 6: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/6.jpg)
Demo: Lightning Chess
![Page 7: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/8.jpg)
Lightning Component Framework
● Lightning Application
● Lightning Component
● Lightning Event
![Page 9: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/15.jpg)
Lightning Chess: Component Communication
Chessboard
Chessboard Location
x64Location Clicked
Location Action
Chess Logic
![Page 16: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/17.jpg)
Component Controller
Component Controller
• Contains Javascript functions
• Bind controller functions in the component markup
• Access component attributes in the controller
![Page 18: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/24.jpg)
Component style
• Component CSS is encapsulated
Component Controller HelperStyle
![Page 25: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/25.jpg)
Component style
![Page 26: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/26.jpg)
Component renderer
Perform your DOM manipulations in the render/re-render functions
Component Controller HelperStyle
Renderer
![Page 27: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/27.jpg)
Component renderer
![Page 28: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/28.jpg)
Calling Apex
Component Controller HelperStyle
Renderer Apex Controller
![Page 29: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/31.jpg)
Overview
• Chess demo
• Lightning component framework
• Lightning components
• Communication between components
• Calling Apex
![Page 32: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/32.jpg)
![Page 33: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/33.jpg)
Go to Developer User Groups!
Belgium: http://bit.ly/1Xqlu5p
The Netherlands: http://bit.ly/1Sbdzuo
![Page 34: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/34.jpg)
thank y uQuestions?
![Page 35: Lightning chess](https://reader036.fdocuments.in/reader036/viewer/2022062310/587199581a28ab044e8b5583/html5/thumbnails/35.jpg)
thank y u