如何在使用类型处理程序和 RemoteObject 创建元素后执行函数?

How to execute function after element creation using type handler and RemoteObject?

我想执行函数`doAfterCreation()?在 CustomTextWidget 实例创建之后。我该怎么做?

function CustomTextWidget(properties) {
    var parent = rap.getObject(properties.parent);
    this.textMask = properties.textMask;

    this.maskedInput = document.createElement('input');
    this.maskedInput.setAttribute('class', 'megaClass');
    parent.append(this.maskedInput);
}

CustomTextWidget.prototype.setTextMask = function (data) {
    this.textMask= data;
};

rap.registerTypeHandler('com.example.CustomTextWidget', {
    factory: function(properties) {
        return new CustomTextWidget(properties);
    },
    properties: ['textMask']
});

function doAfterCreation() {
    // some code
}

您可以尝试将您的函数注册为 render 侦听器。在处理完整个消息后将调用此侦听器,这个时间点很可能适合您的目的。为了只执行一次该函数,它应该在调用时 de-register 本身。

rap.registerTypeHandler('com.example.CustomTextWidget', {
  factory: function(properties) {
    rap.on('render', doAfterCreation);
    return new CustomTextWidget(properties);
  }
  ...

function doAfterCreation() {
  rap.off('render', doAfterCreation);
  // some code
}

更多详情,您可以查看Web Client Reference