SAPUI5自定义伪事件

Jon*_*ink 3 sapui5

在SAPUI5/OpenUI5中定义自定义伪事件的最佳实践是什么?

例如,假设我想在按下并保持几秒钟时在扩展的sap.m.Button上触发一个事件.

Qua*_*ure 7

我不确定是否还有"最佳实践",我真的认为只有'一种'做法;-)但是我很想学习其他任何一种,所以如果有人可以对此发表评论,请不要犹豫!

我认为一般的想法只是定义你的事件; 然后UI5框架自动生成注册(attach<YourEvent>),取消注册(detach<YourEvent>)和触发事件(fire<YourEvent>)的方法.

例如:

return ControlToExtend.extend("your.custom.Control", {
  metadata: {
    properties: {
      // etc...
    },
    aggregations: {
      "_myButton": {
        type: "sap.m.Button",
        multiple : false,
        visibility: "hidden"
      },
      // etc...
    },
    associations: {
      // etc...
    },
    events: {
      yourCustomEvent: {
        allowPreventDefault: true,
        parameters: {
          "passAlong": { type: "string" }
        }
      }
    }
  },

  init: function() {
    ControlToExntend.prototype.init.apply(this, arguments);
    var oControl = this, oMyButton;
    oMyButton = new Button({ // Button required from "sap/m/Button"
      // ...,
      press: function (oEvent) {
        oControl.fireYourCustomEvent({
          passAlong: "Some dummy data to pass along"
        });
      }
    });
    this.setAggregation("_myButton", oMyButton);
  },

  // etc...
});
Run Code Online (Sandbox Code Playgroud)

希望这能解释一下