Expand my Community achievements bar.

AEM - Sling model - Unable to @Inject on 6.5.12

Avatar

Level 10

All,

 

On 64 the below logic resolved to the node multi n case of [A] or it returned empty in case of [B] but on 65 , this returns the parent node[B] if node multi is not found else it returns as in [A] that is expected. Tried @Named but didn't help. Please let me know if i am missing something

 

 

I want it to resolve to multi only. This was working on 64 and now we migrated to 6.5.12 and it is broken. 

 

<sly data-sly-use.carousel="com.test.aem.core.components.carouselController">

 

@Model(adaptables = Resource.class)
public class CarouselController {

@inject
@Deleted Account
public Resource multi;

 

 

 

The content structure 

For [A] - with "multi" node's presence, ${carousel.multi.path} is the path to "multi" node

[A]

/content/X/..
      /group

        /slides

        /multi

 

For [B] - where "multi" node is missing,  ${carousel.multi.path} is the path to "group" node

[B]

The content structure is 

/content/X/..
      /group

        /slides

 

11 Replies

Avatar

Community Advisor

Hi @NitroHazeDev ,

Have you tied with 

@SlingObject
@Optional
public Resource multi;

ignore if already!

Regards,

Santosh 

Avatar

Level 10

Hi @SantoshSai , thanks for the input, it did not help. I hadn't tried it per say until you brought it out but in vain
@SlingObject
@Optional

Avatar

Community Advisor

@NitroHazeDev Is it possible to pass piece of code from Model class? I wan't to see if is there any process with Resource multi variable.

Thats correct resource path will be exactly where current component is used. If your Resource is not pointing to a valid AEM Resource. You must make sure that you are referencing something in the JCR to which the model binds to.

In your multifield example here- the model binds to the products node in the dialog that represents the multi-field node.

@Model(adaptables = Resource.class)
public class CarouselController {

// Inject the multi node under the current node
@Inject
@Optional
public Resource multi;
// No need of a post construct as we don't have anything to modify after the
// model is constructed
}

Avatar

Level 10

I have a workaround , testing if the node multi is with a name "multi" and passing the boolean to the html class from model,   but not super happy about it, what would you like me to test @SantoshSai 

Avatar

Community Advisor

@NitroHazeDev I would rather prepare those ${carousel.multi.path} paths in backend and pass it to HTL to segregate business logic and HTL

Avatar

Level 10

@SantoshSai Those paths are set in the dialog and no manipulation is done to the paths. The multi as in the original thread is referring to the root node or the multi node based on scenarios mentioned above [A] and [B]
Below code is exactly that returns the root node if multi is not available or multi node if it is ,when i print multi.path in html. There is no post construct.
Given the structure

carousel1/

      slides

      multi
if there is no multi node as a child node to carousel ,the controller.multi.path in html returns  the path to carousel1 else it returns path to multi.

My concern is  why it returns the carousel node path if no multi node is present.

Doing a @inject injects it as a child resource or should i specifically specify it as a child resource?

 

Hope this helps. 

 

@Model(adaptables = Resource.class)
public class CarouselController {

// Inject the multi node under the current node
@Inject
@Optional
public Resource multi;
// No need of a post construct as we don't have anything to modify after the
// model is constructed
}

 

Avatar

Level 10

Trying with 
@Inject
@Optional
@ChildResource(name = "multi")
public Resource multi;

 

Weird this worked on 64 and fails on 6.5.12. Will update if i succeed , this will avert the boolean check as well

@SantoshSai 

Avatar

Community Advisor

@NitroHazeDev 

@ChildResource  works only if the adaptable is the SlingHttpServletRequest

Worth last try 

@Optional
@Self
public Resource multi;

 

Avatar

Level 10

@SantoshSai That did not work , the @Deleted Account, it points to the node that holds a resource type A. The component's HTML with resource type A invokes the model with @childresource and it seemed to work , without adapting to SlingHttpServletRequest. Are we sure we compulsorily need to adapt to  SlingHttpServletRequest? http://www.sgaemsolutions.com/2017/08/deep-dive-on-sling-models-part-1.html 

 

@Model(adaptables = Resource.class)
public class CarouselController {

@inject
@Deleted Account
@ChildResource(name = "multi")
public Resource multi;

 

 

Avatar

Level 10

Hi @SantoshSai  nothing but childresource helped. Adapting to resource itself .. I see many use it online the same way. I can’t seem to understand why it worked in 6.2 and not in 6.5.12 and now wondering if @childresource with adapting to resource is the right way to go although it works for me

 

also what gets adapted to is the carousel parent node and I need it to say, ..yes I found the node multi or not