Model classes vs Interfaces

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

01-08-2018

Hello,

I am slightly new to the world of sling models. More used to JSP and custom taglibs (and implementing my own "idea" of models by using reflections) than models. Whatever i have done with models is limited to simple injections, references and self injections.

Recently i was fooling around with some examples from the sling portal and came across this:

@Model(adaptables=Resource.class) public interface MyModel { @Inject String getPropertyName(); }

Then i started wondering why and when you would ever use interfaces? I tried simple parent child relations and it worked well.  But when would i ever write model interfaces and in which scenarios would i ever use them and how does it work?

Pardon me if the question is too basic.. but just could not get this...

thanks,

Yadhu

View Entire Topic

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

06-08-2018

Cool..

smacdonald2008​ thanks a million for the links.. i did a lot of playing around with models... did some really weird things and it was fun.... but now i am bumping in to something which i am not able to wrap my head around... there is the concept of source and there is a concept of via. @Source is an annotation which helps me select an injector out of available registered injectors if i want to force one.... and @Via helps me change the default injection behaviour in the context of a given injector... Around these i had the following questions....

1. Whats the difference between @Source and @Via - My understanding is @Source tells who injects the value... and it is always in the context of the adaptables in your @model annotation... certain sources will not work it adaptables is resource and certain ones will not work is the adaptables in SlingHttpServletRequest. and within the context of a @Source @via tells how the behaviour should be. Here is a weird example but one that works

    @Inject

    @Source("child-resources")

    @Named("topnav")

    @Via(value = "jcr:content", type = ChildResource.class)

    String text;

Means inject with injector "child resources", name of the node is "topnav" and within that context, go to the child called jcr:content and get the property name called text. I know i could have done it much easily with one line but this is just for discussion sake.

Is my understanding correct so far? If yes, are there more examples you can give me?

2. Is the use of injector specific annotations like @ScriptVariable only to reduce the number of lines of code as they look like nothing but two or three basic annotations packed up together.

3, when would i write a custom injector? Is writing a custom injector to pickup request params from a servlet a good scenario?

4. Now the most interesting question... I came across some models which have something like this on the top @Model (adaptables={Resource.class, SlingHttpServletRequest.class}. this got me thinking... i tried doing this... never is this treated like a resource. the SlingHttpServletRequest is taking precedence as @Self never gives me a resource.. @Inject never gives me the valuemap value. I am forced to add @Via("resource") which proves SlingHttpServletRequest takes precedence... so it brings me to the question of why this is done? what is the real use or is this just a buggy piece of code?

eagerly looking forward to an answer... Playing around with sling models has been real fun.. thanks for the links so far.....