Event handlers from XML fragment not triggered

孤街醉人 提交于 2020-11-25 03:42:40

问题


I'd like to attach a liveChange event to the Input field of the reusable Fragment-based Dialog (Walkthrough Step 19: Reuse Dialogs).

In XML-template HelloDialog.fragment.xml I've added:

<Input
    id = "input-b"
    type = "Password"
    liveChange = ".onLiveChange"
    placeholder = "Enter your password" />

In the fragment's controller HelloDialog.js I've added:

onLiveChange: function (oEvent) {
    const sNewValue = oEvent.getParameter("value");
    this.byId("getValue").setText(sNewValue);
    console.log("sNewValue");
}

Then I set in DevTools a break point in this method and try to type a text in the relevant Input and expect that the break point will be fired but nothing happens.

I've tried to add onLiveChange into the view's controller from where I call this fragment and to the Component.js as well, but still no reaction.

The question is why onLiveChange is not triggered in my case? In SAP Sample: Input - Value Update everything is OK, but they use a regular view, not a fragment-based dialog.


回答1:


In order to enable handling events from a fragment, a controller instance or a plain object should be passed to controller when creating the fragment:

{ // My.controller.js
  onLiveChange: function() {
    // ...
  },
  
  open: function() {
    // ...
    Fragment.load({
      id: oView.getId(),
      name: "demo.my.view.fragment.SomeFragment",
      controller: this, // or a plain object that contains event handlers
    }).then(/*...*/);
  },
}

In the case of the Walkthrough Step 19: Reuse Dialogs:

var oFragmentController = {
  // ...
  onLiveChange: function() {
    // ...
  },
};
Fragment.load({
  id: oView.getId(),
  name: "sap.ui.demo.walkthrough.view.HelloDialog",
  controller: oFragmentController, // or `this` if the handlers are defined in the HelloDialog.
}).then(/*...*/);

The same goes for the now-deprecated sap.ui.*fragment:

sap.ui.xmlfragment(oView.getId(), "myFragmentName", this); // deprecated since 1.58.


来源:https://stackoverflow.com/questions/64534777/event-handlers-from-xml-fragment-not-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!