SPA AEM : How can we apply business logic in .model.json output ? | Community
Skip to main content
Level 2
March 5, 2020
Solved

SPA AEM : How can we apply business logic in .model.json output ?

  • March 5, 2020
  • 6 replies
  • 8228 views

Requirement is to show components of Page-A to specific user group therefore we need to apply business logic where model.json is getting generated ? Any pointers on how to do this ?

Currently when /content/xyz/en.model.json is generated, it checks components on each page and call the sling model for those component. Before calling, i want to check some user info and then call the sling model else skip it.

 

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 navjots90210021

The root .model.json is getting rendered by ComponentExporter. This is different than normal jackson exporter. So, the business logic can go into

getExportedType() that is in each sling model. if we return NULL from here instead of resource type, the component do not renders on Front End.
This would require un-caching of root .model.json if some properties needs to be checked on each request.

 

I am looking now versioning of this root .model.json and use selector in order to cache .model.json with different names for diff req types. I am unable to actually get how root .model.json is invoking sling models. If I use different selector in sling model say 'selector1', still the model gets called with '.model' selector.
Update : Versioning can be achieved with .model.group1.json, Aapache can read some cookie, redirect request of .model.json to  .model.group1.json, and the sling model will have request selectors .model.group1.

Thanks @briankasingli  for looking into this. 

6 replies

Level 2
March 5, 2020
One way is to add rep:cugPolicy node under the content/../componentX to restrict to some group. Dn't know any performance issues with this. But looking something that can be done by Business Authors.
BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
March 5, 2020

@navjots90210021,

Apache Sling Models currently includes a single exporter, using the Jackson framework, which is capable of serializing models as JSON. Adobe's new core components are built with Sling Models, meaning that if you can easily build a headless AEM solution only using the core components. If you are using Adobe's core page component, and editable templates, you can replace ".html" with ".model.json", and you will get a JSON representation of the page structure (resourceType & all used components); assuming that you're Apache Dispatcher module rules allow you to access .model.json. e.g: https://example.com/home.model.json

Then there's no magic happening with the Jackson Exporter; all getter properties of your Sling Models class will exposed, and serialized to JSON. This means that if you run some kind of logic in your @PostConstruct method, then set the property, the computed value will be exposed in your JSON.

Take the example below:

 

 

 

 

@Model(adaptables = Resource.class, resourceType = ComponentExample.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) @Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION) public class ComponentExample { protected static final String RESOURCE_TYPE = "my-site/components/content/componentexample"; @ScriptVariable private Page currentPage; private String pagePath; private String logicTitle; @PostConstruct public void init() { if (YOUR_CONDITIONAL_LOGIC_GOES_HERE) { pagePath = currentPage.getPath(); setLogicTitle(); } } private void setLogicTitle() { // logic goes here logicTitle = "results"; } public String getPagePath() { return pagePath; } public String getLogicTitle() { return logicTitle; } }

 

 

 

 

When you append ".model.json" to your page (created with Adobe core components), if you have this component exist on the page && if YOUR_CONDITIONAL_LOGIC_GOES_HERE == true, then you will see the JSON response is:

 

{ pagePath: 'path of page', logicTitle: results', }

 

 

I can't stress to always add unit tests with your Sling Models. A great example that I like to share is this example - https://sourcedcode.com/aem-sling-models-unit-test-junit-4-with-examples

I hope this helps,

Brian.

 

Level 2
March 5, 2020

Sling model exporter will export all keys, I think the only thing we can control is values for those keys. I thing you meant this ? This would require additional check in UI in order to render the component. But if the sling model itself does not export anything, then UI will not render the component. Is there any way we can make sling model do not export anything based upon if condition ?

 

Its B2B and traffic would be very low, so no caching of model.json as of now.

 

Earlier though of versioning of model json based upon CUG groups but that does not work as we can change one selector i.e change model.json to group1.json; but can not add multiple selectors.My initial though was use groups as selector and cache the json. Also sling models will look for this selector and generate the content.

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
March 5, 2020
rep:cugPolicy are done to Pages, Folders and Assets, but I don't recall it being able to components itself.
Level 2
March 5, 2020
What I am trying to achieve is CUG at component level ? If I am not using SPA framework, I can do same using SDI and do things in sling model.
Level 2
March 5, 2020
The spa core page component might be rendering the .model.json , and then calls component sling models. There is no documentation on that.
navjots90210021AuthorAccepted solution
Level 2
March 6, 2020

The root .model.json is getting rendered by ComponentExporter. This is different than normal jackson exporter. So, the business logic can go into

getExportedType() that is in each sling model. if we return NULL from here instead of resource type, the component do not renders on Front End.
This would require un-caching of root .model.json if some properties needs to be checked on each request.

 

I am looking now versioning of this root .model.json and use selector in order to cache .model.json with different names for diff req types. I am unable to actually get how root .model.json is invoking sling models. If I use different selector in sling model say 'selector1', still the model gets called with '.model' selector.
Update : Versioning can be achieved with .model.group1.json, Aapache can read some cookie, redirect request of .model.json to  .model.group1.json, and the sling model will have request selectors .model.group1.

Thanks @briankasingli  for looking into this.