Skip to Content
0

Cannot read property 'call' of undefined

Apr 11 at 08:15 AM

26

avatar image

Hello,

I'm trying to do some operations after a successful data binding of the view. The best idea would be in my opinion to do it in the callback of the attachModelContextChange method. After defining which method would be the callback and attaching the event listener to the view, when I try to bind data to my view, I get the error: Cannot read property 'call' of undefined. I'm pasting my controllers code fragments below and the stack trace.

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);
}


The console.log from the modelCallback method is launching properly. Removing the line with

this.getView().bindElement(sEntityPath);

prevents the error from triggering (but this way I'm of course not getting any data in my view).

Any ideas what am I missing?

Below I'm pasting the stack trace:

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)

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Jamie Cawley
Apr 11 at 12:51 PM
0

Try passing a reference to the function

var myRef =  this.modelCallback.bind(this, 'edit');
this.getView().attachModelContextChange(null,
     myRef,
     this.getView()
);

and use the same var reference in the detach.

Regards,

Jamie

Share
10 |10000 characters needed characters left characters exceeded
Arek Skubala Apr 16 at 10:27 AM
0

This attached the method correctly, but the problem now is - how to detach this method properly? In this version, console.log on this.getView().mEventRegistry will return a growing array (with the key modelContextChange), since the array grows infinitely every time the event is being attached to it. I've tried:

var myRef1 =  this.modelCallback.bind(this, 'edit');
this.getView().detachModelContextChange(myRef1,this);

var myRef2 =  this.modelCallback.bind(this, 'view');
this.getView().detachModelContextChange(myRef2,this);

But it didn't work as desired (the array is still growing).

Share
10 |10000 characters needed characters left characters exceeded