Sling Model instantiation AEM 6.3 | Community
Skip to main content
sukumard
Level 3
November 16, 2017
Solved

Sling Model instantiation AEM 6.3

  • November 16, 2017
  • 4 replies
  • 3829 views

Hi,

As per my observation on AEM 6.3, when we drag and drop our components, the sling model associated with component instantiates immediately.

my Sling Model  is like this :- @Model(adaptables = SlingHttpServletRequest.class) with injectors like below

@Inject @Via("resource")

    @Optional

    private String message;

Is this observation is correct ? if yes, is there any way to stop this instantiation and just show the placeholder and upon authoring in dialog, create the instance of the model ?

Thanks in advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by edubey

AFAIK,  it's the correct observation.

You would probably have to add check in your @PostConstruct method Apache Sling :: Sling Models or HTL  (formerly known as Sightly)

4 replies

edubey
edubeyAccepted solution
Level 10
November 16, 2017

AFAIK,  it's the correct observation.

You would probably have to add check in your @PostConstruct method Apache Sling :: Sling Models or HTL  (formerly known as Sightly)

sukumard
sukumardAuthor
Level 3
November 20, 2017

hi,

@PostConstruct will be called after all injections , while i want to stop instantiation of sling model during drag and drop.

edubey
Level 10
November 20, 2017

I don't think so you can do it.

Best case, you can try to add a check on the sightly element where you add this class.

Thanks

atyaf66
Level 2
November 20, 2017

If you wrap your data-sly-use inside a data-sly-test that evaluates to false it will not be evaluated.

for example, let's say you have a simple text component with one simple authored property "simpleText"
You want to NOT instantiate the model if  "simpleText" is NOT authored

you can do the following

```

<!-- NOTE the use of data-sly-test on the same element as the  data-sly-use -->

<sly data-sly-test="${properties.simpleText}" data-sly-use.TextModel="package.path.to.model.Text"/>

<div>

    <h1>placeholder</h1>

    <h1>text is: ${properties.simpleText}</h1>

</div>

```
The model will only instanciate if simpleText is authored and will not otherwise.

you can always make a simple component and test.