CQ5 AEM:如何使用 javascript 在对话框中按名称获取组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22547040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CQ5 AEM: how to get a component by name within a dialog using javascript
提问by Alisneaky
I know this will probably a simple question, but I'm new to CQ5 and AEM in general.
我知道这可能是一个简单的问题,但总的来说我是 CQ5 和 AEM 的新手。
I have a cq:Widget node which is a simple textfield.
我有一个 cq:Widget 节点,它是一个简单的文本字段。
<rowtitlevalue
jcr:primaryType="cq:Widget"
fieldLabel="Row Title Value"
name="./rowtitlevalue"
xtype="textfield"
disabled="true"/>
Now at the moment within my JavaScript, I'm currently accessing it via
现在在我的 JavaScript 中,我目前正在通过
var textfield = panel.findByType('textfield')[1];
which works fine (there's another textfield before this one, hence the 1 in the array.
这工作正常(在这个之前还有另一个文本字段,因此数组中的 1 。
MY QUESTION: how do I look for this field using it's NAME attribute within my javascript.
我的问题:我如何在我的 javascript 中使用它的 NAME 属性查找这个字段。
Any help would be appreciated.
任何帮助,将不胜感激。
Also, I'm using this object to run the following:
另外,我使用这个对象来运行以下内容:
if (show != undefined) {
textfield.enable();
textfield.show();
}
else if (show == undefined) {
textfield.disable();
textfield.hide();
}
The JavaScript is located within the Component Based ClientLibs.
JavaScript 位于基于组件的 ClientLibs 中。
And this is the Listener that I have under the checkbox that defines the value of SHOW within the javascript (which is working fine).
这是我在定义 javascript 中 SHOW 值的复选框下的监听器(工作正常)。
<listeners
jcr:primaryType="nt:unstructured"
loadcontent="function(field,rec,path){Ejst.toggleRowTitle(field);}"
selectionchanged="function(field,value){Ejst.toggleRowTitle(field);}"/>
Please let me know if you see any problems with this.
如果您发现这有任何问题,请告诉我。
Appreciate it in advance
提前欣赏
回答by rakhi4110
The CQ.Dialog APIdefines the getField( String name)
method which returns a field with the given name. In case more than one field with the same name exists, it returns an array of those fields.
所述CQ.Dialog API定义getField( String name)
,它返回一个字段具有给定名称的方法。如果存在多个同名字段,则返回这些字段的数组。
Thus finding parent of xtype dialog
instead of panel
as shown below would solve this.
因此,找到 xtype 的父级dialog
而不是panel
如下所示可以解决这个问题。
Ejst.toggleRowTitle = function(checkbox) {
var dlg = checkbox.findParentByType('dialog');
var rowTitleField = dlg.getField('./rowtitlevalue');
// perform required operation on rowTitleField
}
回答by Phoenix_uy
i did something like that a few days ago and my solution was to make a js
file on the same level of the component and with the same name of the component with the information that i need.
Something like this:
The file will be called rowtitlevalue.js
and the content would be:
几天前我做了类似的事情,我的解决方案是js
在组件的同一级别上制作一个文件,并使用我需要的信息与组件的名称相同。像这样:文件将被调用rowtitlevalue.js
,内容将是:
"use strict";
use(function() {
var data = {};
data.rowtitlevalueData = properties.get("rowtitlevalue");
//more properties if needed...
return {
data: data
}
});
then where i need to use it in javascript, i need the following tags:
那么我需要在javascript中使用它的地方,我需要以下标签:
<sly data-sly-use.data="rowtitlevalue.js">
<script type="text/javascript">
var myVariable = ${data.rowtitlevalueData};
</script>
</sly>