SlingModel
Sling Model:
Component With Sling Model: 1.Front End 2.Back End
Front End Contain all the Rendering Logic you have and the Back End Controller will process the business logic and return data to Front End.
Front End:
To Create Component we need to follow the following steps.
author.html and cq:dialog are mandatory fields ,this is the starting point of rendering Logic, these dialogs take input from content author and display on page.
clientlibs: If you have any ui specific like javascript, css, svg and other stuff will be in clientlibs, you should have those component related stuff in your component itself. It’s adobe Recommended. You can have any number of clientlibs in your component.
cq:design_dialog: this is same as cq:dialog but the only difference is the design_dialog save your values at Site level(means at a common place), So these values or properties available through-out the site. but cq:dialog values or properties available only on that component node or page. And these cq:design_dialog is only used for Static templates, for editable templates or dynamic templates this dialog is not used.
Cq:editConfig: it’s optional and it defines more Behaviour in Author mode.
Back-End:
We have multiple options to write back end controller:
1.JavaScript Controller: we can simply write java script with in the component(clientlibs). It is not recommended but optional you can write.
2.WCMUse Class(WCMUsePojo):these controller is being used since the slightly came into picture. These is not recommended once the sling-model came.
Sling Model:
The interface class is not mandatory but it is AEM recommendation.the author.java interface class Is implemented through AuthorImpl.java implementation class
In author.html your should write slightly(<div data-sly-use.object=“com.projectname.aem.core.models.classname”> …..</div>) to call backend controller.
Adaptable’s: its defines which source object can be adapted to sling model
Adapters: an adapter is basically a method called adaptive and this method is used everywhere in AEM, in this adapters we need to give interface name, because we have written our own interface right!?.
Author.java:(Interface class)
package com.myproject.aem.core.models;
public interface Author {
String getFirstName();
String getLastName();
boolean getIsProfessor();
}
AuthorImpl.java:
package com.myproject.aem.core.models;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.apache.sling.models.annotations.Required;
import javax.inject.Inject;
@Model(adaptables = Resource.class,adapters = Author.class)
public class AuthorImpl implements Author{
@Inject
@Default(values = "RED")
public String fname;
@Inject
@Optional
@Default(values = "REV")
public String lname;
@Inject
@Required
public boolean professor;
@Override
public String getFirstName() {
return fname;
}
@Override
public String getLastName() {
return lname;
}
@Override
public boolean getIsProfessor() {
return professor;
}
}
Now build this —> after build successful
Go to your page and add this component and author it. The values given in dialog will reflect to page that’s it.


Comments
Post a Comment