» SampleModel
package com.flagtick.core.models;
public interface SampleSlingModel {
String getResourcePage();
}
» SampleModelImpl.java
package com.flagtick.core.models.impl;
import com.day.cq.wcm.api.Page;
import com.flagtick.core.models.SampleSlingModel;
import com.flagtick.core.utils.LinkUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import javax.inject.Inject;
@Model(adaptables = {SlingHttpServletRequest.class, Resource.class},
adapters = SampleModel.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class SampleModelImpl implements SampleModel {
@Inject
private Page resourcePage;
@Override
public String getResourcePage() {
return resourcePage.getPath();
}
}
Note: Please using Resource.java attached and get page resources.
» sample.html
...
data-cmp-is="Sample"
data-path="${model.resourcePage}"
data-editmode="${wcmmode.edit ? 'true' : ''}"
...
Note: Always position `data-cmp-is` attribute at the top of all HTML data attributes.
» Sample.ts
...
private static readonly SAMPLE_ICON = 'flagtick-icon--eye';
private selector = 'sample';
private apiKey!:string; // assure that the variable will be assigned a value before it is used
private $buttons;
constructor(cmp: HTMLElement) {
super(cmp);
this.$component = $(cmp);
this.$buttons = cmp.querySelectorAll('.flagtick-sample__button');
...
const currentPage = this.$cmp.dataset.path as string;
const endpoint = `${resource}.${this.selector}.json?currentPage=${currentPage}`;
...
}
Note: You can call restAPI from Sample component.