Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Sling Model instantiation AEM 6.3

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Correct answer by
Level 10

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)

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

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)

Avatar

Level 3

hi,

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

Avatar

Level 10

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

Avatar

Level 2

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.