Search This Blog

Wednesday 31 May 2017

Sling models and WCMUsePojo



  • You should use one or the other - if you use WCMUse/WCMUsePojo - then do not use SLing Models. If you want to use SLing Models - then do not use WCMUse/WCMUsePojo.
  • Both provide the same functionality so no use of using both.
  • WCMUse is deprecated in 6.1. Use WCMUsePojo instead.

Now let’s look at the implementation for this, you have 5(!) options:
  1.  Class that implements the Use interface (deprecated)
  2.  Class that extends WCMUsePojo class
  3.  Class that is adaptable from Resource (resource.adaptTo(YourClass))
  4.  Class that is adaptable from Request (request.adaptTo(YourClass))
  5.  Sling Models
Both 3,4 are usefull when you implement AdapterFactory interface. These 2 can be ignored.


Inject annotation issue in Sling models AEM 6.2

Read here - http://aemconcepts.blogspot.com/2017/07/sling-model-inject-annotation-issue-in.html

Sling models


  • Sling models can be created using model annotation which automatically adapts your request or resource.
  • Doesn’t provide any activate method. Activate method is treated just like another method.
  • Method have @PostConstruct annotation does not need to be called using a use object. It gets called automatically after all injections are done. Once you edit and save dialog injections happen again hence method with this annotation is called again.
  • Style object currentStyle is also only available through the SlingHttpServletRequest as an injection and not thru adapting to a resource. But in adaptable from resource we can get it by adapting resource.

Available injectors for sling models.


 Inject resource resolver

    @Inject
    private ResourceResolver resourceResolver;


 @named annotation

Injects are injected as instance variables with same name as the property. Both if you want different property name and instance name then this annotation can be used. For example
      /** The news filter tags. */
      @Inject
      @Named("newsfiltertags")
      private String[] newsFilterTags;

This inject newsfiltertags property value in newsFilterTags instance variable.


Sling model adaptable from request

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MegaMenuModel {…}

Retrieve resource


To retrieve resource.

  1.          request.getResource();
  2.          Inject
      @Source("script-bindings")    [optional]  private Resource resource;


 To inject Style for design

      @Inject
      private Style currentStyle;


Sling model adaptable from resource


@Model
(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public
class LoginSplashScreenModel {…}


Get Style object to retrieve from design


TagManager = resourceResolver.adaptTo(TagManager.class);
designer = this.resourceResolver.adaptTo(Designer.class);
if (designer != null) {
      style = designer.getStyle(resource);
}
                 
if (StringUtils.isEmpty(viewAllTitle)) {
      viewAllTitle = style.get("viewalltitle", String.class);
}

Passing Parameters from slightly to sling models.

To show the full range of possibilities, let’s also pass a parameter, so the content of the data-sly-use attribute must be changed to an expression with an option:
<div data-sly-use.myComponent="${'com.myproject.MyComponent' @ param1='one', param2='two'}">
    ${myComponent.calculatedValue}
</div>



WCMUsePojo/WCMUse

·       Provides convenience method activate() which is called from wcmusepojo.init() once all injections are done. Implement this method to perform post initialization tasks.

·      Method activate does not need to be called using a use object. It gets called automatically after all injections are done.

@Override
activate(){..}

2 comments:

  1. This post seems so Informative for all Adobe CQ users. My friend is now taking Adobe CQ training at "ExcelR Solutions". I think this post will give him a good information for his training. I will share this post with him and hope he will like it.

    ReplyDelete
  2. Awesome. I was banging my head, because of Inject dependency issue caused by the of the addition of Sling Model API, finally I found the solution here under "Inject annotation issue in Sling models AEM 6.2"

    ReplyDelete