Search code examples
eclipse-rap

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


I want to execute function `doAfterCreation()? after CustomTextWidget instance creation. How can I do that?

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
}

Solution

  • You could try to register your function as a render listener. This listener will be called after the entire message has been processed, chances are that this point in time is suitable for your purpose. In order to execute the function only once, it should de-register itself when it's called.

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

    For more details, you can have a look at the Web Client Reference.