Dynamic Components using Single-Page-Application Concepts in AEM/CQ

22
APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 Dynamic Components using SPA Concepts Andon Sikavica, Bojana Popovska

description

Dynamic components display content dependable on context, hence they cannot be cached. Out of the box, Adobe Experience Manager doesn't give us many options for granular caching on a component level. When faced with this problem, we usually resort to developing components that are leveraging Server-Side Includes or AJAX to get the HTML with dynamic data. As an alternative solution, we have also developed dynamic components that use Single Page Application concepts, by using templates and JSON-responses, to provide the same dynamic behavior. In this presentation we will cover all of the dynamic components types, compare the benefits and drawbacks of each, and state the use-cases where each can be effectively applied. We will take a deeper look at the dynamic components done with SPA concepts, as they are rarity in the AEM world, and also provide a walk-through of the technologies used, how some common problems were solved, as well as the benefits that have been gained by their usage.

Transcript of Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Page 1: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

APACHE SLING & FRIENDS TECH MEETUPBERLIN, 22-24 SEPTEMBER 2014

Dynamic Components using SPA ConceptsAndon Sikavica, Bojana Popovska

Page 2: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

adaptTo() 2014 2

Dynamic Components

Page 3: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Dynamic vs Static Components

adaptTo() 2014 3

Initial and form-based components take big chunk

of the statics

We spend more than 90% of the time developing

dynamics

Count of Dynamic vs Statics(in hundreds)

Dynamic Static

Page 4: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Dynamic Component Development

adaptTo() 2014 4

Placeholder for dynamic component in dispatcher think about what you will have in the dispatcher

Request dynamic component via URL /content/page/_jcr_content/par/comp.dynamic.html

Instruct dispatcher not to cache based on selector #/0042 { /type "deny" /glob "GET *.dynamic.html*" }

Page 5: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Server Side Includes

adaptTo() 2014 5

Browser Dispatcher AEM

GET Page

return complete Page

GET dynamic component HTML

return component HTML with dynamic data

Generate HTML

<!--#include virtual=“${c.resourcePath}.dynamic.html}”-->

Page 6: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

AJAX

adaptTo() 2014 6

Browser Dispatcher AEM

GET Page

return Page with Placeholders

GET dynamic component HTML

return component HTML with dynamic data

Generate HTML

Create Markup

$.ajax({url: '${c.resourcePath}.dynamic.html',success: function(data) {$(“#${c.identifier}").replaceWith(data);

}});

Page 7: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Summary

adaptTo() 2014 – https://www.flickr.com/photos/fabrisalvetti/489593502 image by Fabrizio Salvetti 7

Sling Concepts component-path selectors sling:resourceSuperType

Aspect-Oriented Solution Sling Dynamic Include

Page 8: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

What if…

adaptTo() 2014 8

… you have high number of dynamic components on a single page?

… your components need to communicate between themselves and update their state?

… you need to do notifications for the user?…basically need SPA

Page 9: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Why were the usual approaches Tricky?

adaptTo() 2014 - https://www.flickr.com/photos/15708236@N07/2754478731 photo by jphilipg 9

They were meant to be used with page reloads

They work with pre-loaded Markup from server

Page 10: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

adaptTo() 2014 10

Single Page Application

Page 11: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

SPA Component

adaptTo() 2014 11

Rendering Script (JSP) Cached, Inherited

Module (JS) Cached, Composition

Controller Handles all dynamic calls

Table

Abstract

Paging

Sorting

Page 12: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Page Load

adaptTo() 2014 12

Browser Dispatcher AEM

GET Page

return Markup Skeleton

POST for Dynamic Data

return Dynamic Data as JSONSerialize Data as JSON

Render HTML using Template

with dynamic data

Page 13: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Technologies

adaptTo() 2014 13

jQuery jQuery loadTemplate GSON Spring (MVC, AOP)

Page 14: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Benefits

adaptTo() 2014 - https://www.flickr.com/photos/mishism/5370473007 photo by Mish Sukharev 14

Perceived User Experience Separation of front- and

back-end engineering easier development prototype mocked data

Page 15: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

adaptTo() 2014 15

Showcase

Page 16: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Rendering Script

adaptTo() 2014 16

<script type=“text/html”

id=“scheduleTableTemplate”>

<tr>

<td data-content=“talkName”></td>

<td data-content =“time”></td>

</tr>

</script>

<script type=“text/javascript”>

aemdemo.scheduletable.init(

‘http://localhost:4502/bin/mvc.do/scheduletable/getresult’);

</script>

$(‘.scheduleTable tbody’).loadTemplate(

$(‘#scheduleTableTemplate’),

data.talks);

<table

class=“scheduleTable”>

<th>

<td>Talk Name</td>

<td>Time</td>

</th>

<tbody></tbody>

</table>Template

Static Markup

Initialization script

Inject data

Page 17: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Module

adaptTo() 2014 17

aemdemo.schedulestable = (function($, paging, sort){

var params;

function init (dataUrl){

params.dataUrl = dataUrl;

}

function getDataByPage (pageNumber) {

$.ajax {

data : {. . .},

url : params.dataUrl,

success : function (data){paging.update (data)}

}

}

return {

init:init

}

}(jQuery, aemdemo.paging, aemdemo.sort));

Page 18: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Controller

adaptTo() 2014 18

@Controller

public class ScheduleTableController {

@RequestMapping(value = "/scheduletable/getresult",

produces = {"application/json" }, method = POST)

@ResponseBody

public String getResult(@RequestParam int pageNumber) {

ConferenceDay conferenceDay = getDataFromService();

return serializeData(conferenceDay);

}

}

Page 19: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Identify and Control POSTs

adaptTo() 2014 19

Add Header$.ajaxPrefilter(function (options, originalOptions, jqXHR) {

jqXHR.setRequestHeader('X-Request-Source', ‘spa-ajax');

});

Check HandlerServlet servlet = servletResolver.resolveServlet(slingRequest);

if (“SlingPostServlet”.equals(servlet.getClass().getSimpleName()

&& isDynamicRequest() ) {

// ups

}

Page 20: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

adaptTo() 2014 20

http://bit.ly/aemspademo

Demo Project

Page 21: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Questions

adaptTo() 2014 21

Page 22: Dynamic Components using Single-Page-Application Concepts in AEM/CQ

Thank You!

adaptTo() 2014 22