1
使用JavaScript使用-Api我可以创建自定义对象并将其返回给html文件。此功能允许我创建自定义对象的列表,可用于创建菜单或其他复杂的类列表组件。CQ5/AEM6/Sightly - 从Java返回自定义类型使用-Api
让我们假设我有以下内容构成:
/content
/project
/homepage
/contentpage1
/contentpage1.1
/contentpage1.2
/contentpage1.3 (hidden)
/contentpage2
/contentpage1.1 (hidden)
/contentpage1.2 (hidden)
/contentpage1.3 (hidden)
/contentpage3
/contentpage4
菜单应只包含第一级的内容网页。每个菜单项都应该包含第二级内容页的下拉列表,如果它们存在并且未隐藏。我可以用下面的代码做在JavaScript:
"use strict";
use(function() {
function getMenuItems() {
var currentPageDepth = currentPage.getDepth();
var menuObjects = [];
if(currentPageDepth >= 3) {
var homePage = currentPage.getAbsoluteParent(2);
var list = homePage.listChildren();
while(list.hasNext()) {
var tempPage = list.next()
var customPageObject = createMenuItemObject(tempPage);
menuObjects.push(customPageObject);
}
}
return menuObjects;
}
function createMenuItemObject(page) {
// ...
// looking for any other properties of page or its children
// ...
return {page: page,
visibleChildrenExists: visibleChildrenExists(page)};
}
function visibleChildrenExists(page) {
var list = page.listChildren();
var visibleChildrenExists = false;
while(list.hasNext()) {
var subPage = list.next();
if(!subPage.isHideInNav()) {
visibleChildrenExists = true;
break;
}
}
return visibleChildrenExists;
}
return {
menuObjectsList: getMenuItems(),
};
}
HTML:
<headerComponent data-sly-use.headerComponentJS="headerComponent.js" data-sly-unwrap />
<menuItems data-sly-list.menuItem="${headerComponentJS.menuObjectsList}" data-sly-unwrap >
<li class='${menuItem.visibleChildrenExists ? "" : "direct"}' data-sly-test="${!menuItem.page.hideInNav}">
<a href="${menuItem.page.path}.html">${menuItem.page.title}</a>
<ul data-sly-test="${menuItem.visibleChildrenExists}" data-sly-list.submenuItem="${menuItem.page.listChildren}">
<li data-sly-test="${!submenuItem.hideInNav}">
<a href="${submenuItem.path}.html">${submenuItem.title}</a>
</li>
</ul>
</li>
</menuItems>
为什么我要使用Java使用的API?在资源或节点等界面上操作更容易。它看起来在JavaScript中不能很好地工作,但我需要有可能返回具有多个属性的自定义对象。
问题是:是否有可能使用Java Use-Api做类似的事情?我必须返回什么?我无法返回地图,因为无法将参数传递给Java Use-Api方法,因此无法访问它的元素......任何建议?