attachEventOnce() 导致 "undefined is not a function"

attachEventOnce() leads to "undefined is not a function"

best practice guide 之后我得到一个错误 "Uncaught TypeError: undefined is not a function"。

通过很多 console.logs 我发现在以下代码行中抛出了这个错误(在控制台中是匿名的):

oView.getElementBinding().attachEventOnce(....)

AttachEventOnce() 是导致问题的函数。您知道如何解决这个问题或进一步调试吗?

代码块:

        oView.getElementBinding().attachEventOnce("dataReceived", jQuery.proxy(function () {

            var oData = oView.getModel().getData(sReportPath);
            if (!oData) {
                sap.ui.core.UIComponent.getRouterFor(this).myNavToWithoutHash({
                    currentView : oView,
                    targetViewName : "namespace.view.NotFound",
                    targetViewType : "XML"
                });
            }
        }, this));

引自最佳实践指南:

          // Check that the product specified actually was found
          oView.getElementBinding().attachEventOnce("dataReceived", jQuery.proxy(function() {
              var oData = oView.getModel().getData(sProductPath);
              if (!oData) {
                  sap.ui.core.UIComponent.getRouterFor(this).myNavToWithoutHash({
                      currentView : oView,
                      targetViewName : "sap.ui.demo.tdg.view.NotFound",
                      targetViewType : "XML"
                  });
              }
          }, this));

这整个事情在 "routeMatched" 部分被触发。

attachEventOnce() 似乎不是 JavaScript 的标准部分(您可以通过打开 JS 控制台并选择页面上的任意元素来验证这一点,并且 运行 object.attachEventOnce() === undefined;.) 我打赌这个标准指南在文本的前面定义了它。看来 one() 可能是您想要的功能。

由于绑定上下文引用了一个特定的实体,它无限地“延迟”(~循环)。

但是,绑定首先使用以下方法:

  • 将所有内容绑定到控件
  • 使用绑定列表中的第一项(无论它是什么)

但是一旦点击不同的控件并因此触发上述代码行,绑定就会更改为特定实体 (http://odataservice/entity('WithASpecificKey'))。

事实是,如果您尚未在 OData 服务中实现“readEntity”方法,就会出现此错误。乍一看似乎很奇怪,因为它适用于初始方法,但在单击后无效。这就是为什么它 was/is 有点像调试。

我所做的是在 OData 服务中正确实现“readEntity”方法。