11

我正在编写“保存”命令的扩展,基本上我想验证某些字段并显示一个弹出窗口,允许编辑器根据当前日期、发布号和其他一些属性选择给定的关键字或其他值。

我以为我进展顺利,直到我最终发现$display.getItem()返回的是存储在 CM 中的项目,而不是编辑器可能已更改的当前值。

是否有内置方法可以让我获取此信息?还是我需要解析 DOM 才能弄清楚?

这是我目前拥有的代码

var item = $display.getItem();
if (item.getItemType() == "tcm:16") {
   if (item.getSchema().getStaticTitle() == "Test Schema") {
      var content = $xml.getNewXmlDocument(item.getContent());
      var fieldXml = $xml.getInnerText(content, "//*[local-name()='NewField']");
      alert(fieldXml);
   }
}

它正在工作 - 我得到“NewField”的值 - 但这是项目加载时的值,而不是当前

有趣的是,item.getTitle()显示了 Title 字段的当前值,所以我希望也有办法为自定义字段实现这一点。

4

2 回答 2

9

我不知道这是否是正确的方法,但您可以在项目上触发“collectdata”事件 - 这将使用迄今为止在编辑屏幕上输入的内容更新它的数据。

var item = $display.getView().getItem();
item.fireEvent("collectdata");
$log.message(item.getXml());
于 2012-05-07T09:00:45.417 回答
3

Peter's approach copies the values from the controls in the HTML into the item's XML. This is a great approach if you don't mind the item being updated, since it allows you to simply manipulate the XML instead of the HTML.

But if you don't want the item to be updated yet, you have no choice but to find the correct control(s) in the HTML and read the value from there.

I wrote up this little helper function for it:

function getControlForFieldName(name)
{
    var fieldBuilder = $display.getView().properties.controls.fieldBuilder;
    var fieldsContainer = fieldBuilder.properties.input;
    var fieldsNode = fieldsContainer.getElement();
    var fieldContainer = $dom.getFirstElementChild(fieldsNode);
    while (fieldContainer)
    {
        var labelNode = $dom.getFirstElementChild(fieldContainer);
        var fieldNode = $dom.getNextElementSibling(labelNode);
        var control = fieldNode.control;
        if (control.getFieldName() == name)
        {
            return control;
        }
        fieldContainer = $dom.getNextElementSibling(fieldContainer);
    }
}

With this function in place you can simply look up the control for a field given its name. When you have the control you can get the values from it easily.

var fieldControl = getControlForFieldName('Body');
if (fieldControl)
{
    var values = fieldControl.getValues();
    // values is an array, since it caters for multi-value fields
    // if this is a single-value field, get the value from values[0]
}

Note that my approach requires way more code than Peter's and touches quite a few non-public APIs.

于 2012-05-07T14:19:29.540 回答