【问题标题】:SAPUI5: Cannot read property 'call' of undefined on elementBindingSAPUI5:无法读取 elementBinding 上未定义的属性“调用”
【发布时间】:2018-09-21 01:13:53
【问题描述】:

在视图数据绑定成功后,我正在尝试执行一些操作。在我看来,最好的办法是在 attachModelContextChange 方法的回调中进行。在定义哪个方法将作为回调并将事件侦听器附加到视图后,当我尝试将数据绑定到我的视图时,我收到错误:无法读取未定义的属性“调用”。我在下面粘贴我的控制器代码片段和堆栈跟踪。

    onInit: function() {
        var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
        oRouter.attachRouteMatched(this.routeMatched, this);
    },


    routeMatched: function(oEvent) {
        var sEntityPath,
            oParameters = oEvent.getParameters();

        this.routeName = oParameters.name;

        this.getView().detachModelContextChange(
            this.modelCallback('edit'),
            this.getView()
        );

        if (oParameters.name === "EditItem") {
            if (!isNaN(oParameters.arguments.id)) {
                this.getView().attachModelContextChange(null,
                    this.modelCallback('edit'),
                    this.getView()
                );
                sEntityPath = "/ItemData(" + oParameters.arguments.id + ")";
                this.getView().bindElement(sEntityPath);
            } else {
                var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
                oRouter.navTo("CreateItem", {
                    from: "EditItem"
                }, false);
            }
        }
    },

    modelCallback: function(type) {
        console.log(type);
    }

modelCallback 方法中的 console.log 正在正常启动。删除与

this.getView().bindElement(sEntityPath);

防止错误触发(但这样我当然不会在我的视图中获得任何数据)。

任何想法我错过了什么?

下面我粘贴堆栈跟踪:

datajs.js:17 Uncaught TypeError: Cannot read property 'call' of undefined
at f.a.fireEvent (sap-ui-core.js:449)
at f.a.fireEvent (sap-ui-core.js:991)
at f.fireModelContextChange (sap-ui-core.js:573)
at f.h.setElementBindingContext (sap-ui-core.js:524)
at constructor.i (sap-ui-core.js:500)
at constructor.a.fireEvent (sap-ui-core.js:449)
at constructor.B._fireChange (sap-ui-core.js:1302)
at eval (library-preload.js:2375)
at u (library-preload.js:2460)
at d.r._processSuccess (library-preload.js:2492)

【问题讨论】:

    标签: javascript binding sapui5


    【解决方案1】:

    attachModelContextChange 调用中的参数不正确。唯一需要的参数是回调函数,在您的情况下为 modelCallback。您将 null 作为 oData 可选参数传递,该参数可以省略。正确的调用应该是这样的:

           this.getView().attachModelContextChange(
                this.modelCallback, // the function, not the result of its call!
                this // Instance of controller (not View!), which is now *this*
            );

    另外,detachModelContextChange 的调用应该使用相同的参数才能使其工作:

    this.getView().detachModelContextChange(
      this.modelCallback, // the function, not the result of its call!
      this // Instance of controller (not View!), which is now *this*
    );
    当您将 this.modelCallback('edit') 放在参数列表中时,首先执行该函数,然后将其结果作为参数传递给 attachModelContextChange(或 detachModelContextChange)

    【讨论】:

    • 好的,问题解决了。但是有没有一种方法可以使这个参数与传递的参数一起工作(我的意图是使用相同的回调方法但使用不同的参数 - 现在我必须使用两个单独的回调方法而不是参数化上面的一个示例)?
    • 是的,您可以: this.getView().attachModelContextChange( your_data, this.modelCallback, // 函数,而不是其调用的结果! this // 控制器实例(不是 View!) , 现在是 this );最好在这里查看:sapui5.netweaver.ondemand.com/#/api/sap.ui.base.ManagedObject/…
    猜你喜欢
    • 1970-01-01
    • 2017-07-04
    • 2018-07-13
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2020-10-30
    相关资源
    最近更新 更多