Expand my Community achievements bar.

SOLVED

Initialize a model class in a different model class

Avatar

Level 3

Hi Community,

 

I have a query, please help me with this question.

 

I am trying to call a get method in a model class which is inside another model class, I will give a simple example below.

I hope the below example is sufficient enough to answer my Question, Thank you for the replies.

 

Class HelloWorldModelImpl:

 

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}, adapters= HelloWorldModel.class, 
resourceType = HelloWorldModelImpl.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class HelloWorldModelImpl {

//
private EmployeeModel employeeModel;

public String getEmpID(){
return employeeModel.getEmpID();
}

public String getEmpName(){
return employeeModel.getEmpName();
}

}

 

Class EmployeeModel:

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

@ValueMapValue
private String empId;

@ValueMaapValue
private String empName;

public String getEmpID(){
return empId;
}

public String getEmpName(){
return empName;
}
}

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Vinod-N-E calling ModelB class methods from ModelA, the below code works for me, please try: 

Note: Just add @Self as well

Once you've modelB object you can call methods.

@Model(adaptables = Resource.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL )
public class ModelA { 
@Inject
@Self
private ModelB modelB;

}

 

View solution in original post

7 Replies

Avatar

Community Advisor

I think it is straight forward,

you can inject a child resource or adapt the current resource to another model

Available injectors  : https://sling.apache.org/documentation/bundles/models.html#available-injectors-1 

 

 

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

	@Self
	Resource resource;

	@Inject
	private LinkModel link;

 

 



Arun Patidar

Avatar

Level 3

Sorry Arun, Your answer is not satisfying. 

 

As, I am trying to understand if I could achieve something like the example I have given. As I tried something like the example I have given, the EmployeeModel class is null while accessing that model class.

Avatar

Community Advisor

In Sling Model, It is possible to call one sling Model from another.

I am not sure about your content structure so can't really help with the actual code but just from concept size its is possible to inject another custom type(sling model) in sling model



Arun Patidar

Avatar

Correct answer by
Community Advisor

@Vinod-N-E calling ModelB class methods from ModelA, the below code works for me, please try: 

Note: Just add @Self as well

Once you've modelB object you can call methods.

@Model(adaptables = Resource.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL )
public class ModelA { 
@Inject
@Self
private ModelB modelB;

}

 

Avatar

Community Advisor

@Vinod-N-E you can also try adapting resource to ModelB

@SlingObject
Resource resource

ModelB modelB = resource.adaptTo(ModelB.class)


Hope this helps,
Krishna