Sightly support - 4.2
Slice makes development of your AEM components easier and simpler than before. It perfectly integrates with Sightly creating a great tandem for you application development.
Example component
Take a look at the following example:
<div data-sly-use.model="com.example.app.ArticleModel"> ${model.text} </div>
At the first glance it looks like a standard use of Sightly and its Java Use-API. And from the Sightly point of view that's absolutely true - you don't need to write anything in your Sightly templates to use Slice!
There is a couple of ways you can use Java Use-API (implementation of Use interface, extension of WCMUse, adapting from resource, adapting from request) and Slice uses one of them to make integration absolutely seamlessly for end user. Therefore your Java objects are simple POJOs and you don't need to write any additional code. In our example, the ArticleModel
will look as follows:
@SliceResource public class ArticleModel { @JcrProperty private String text; public String getText() { return text; } }
Please note that you don't need to implement Use interface, nor extend WCMUse class, nor creating adapter factory. The only thing you need to do is to put @SliceResource
annotation for your class. Simple.
Slice adapter factory
You may wonder how does it work then? Basically, Slice hides use of Java Use-API and creates adapter factory for every class annotated by @SliceResource
. The adapter factory is able to adapt a resource to your model - it uses standard ModelProvider to get an model from provided resource.
When an injector is started, Slice scans all of your classes and creates a SliceAdapterFactory object for every SliceResource model. Afterwards, the factory object is being registered as a OSGi service which allows Sightly's Use-API to make its work.
Benefits
Why should you use Slice if you have Sightly? Well, there is couple of reasons:
- You don't need to write adapter factories or implement Use interface - Slice does it for you. Your code is simpler and can be developed quicker.
- You can use the real power of Slice - the dependency injection - and combine your models with business objects easily. Your code will be more testable and maintainable.
- You can reuse your Java models in Sightly and JSP templates without any modification