sapui5jsonmodel

Error while setting model inside onInit() of root view - no error / no log


I am trying to set a new JSONModel on the Main.view.xml (root view). But it seems like it is stoping at .setModel(). The console.log("after") is not logging.

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "jquery.sap.global",
    "sap/m/MessageToast",
    "sap/ui/model/json/JSONModel"
   ], function (Controller, JSONModel, MessageToast) {
   "use strict";
   return Controller.extend("sap.ui.bookedTimes.wt.controller.Main", {       
        onInit   : function () {            
            var jModel = this.getOwnerComponent().getModel("zCatsTestJ");
            var that = this;    
            jModel.attachRequestCompleted(function() {                  
                console.log(that.getView());
                var oViewModel= new JSONModel({workdate: "test"});
                console.log("before");
                that.getView().setModel(oViewModel, "view");
                console.log("after");
                console.log(that.getView().getModel("view"));    
            });
        },
   });
});

Error in console

Entry in manifest.json:

"sap.ui5": {
    "rootView" : {
        "viewName":"sap.ui.bookedTimes.wt.view.Main", 
        "id": "mainview",                                              
        "type": "XML"                                  
     },

Is there a problem in onInit() of the root view?

Update: I should have added the part of the xml.view. I changed the view name to "view1" and everything from the controller was logged. The problem was that my view was still expecting a date

<Text text="{ path: 'view1>/workdate', type: 'sap.ui.model.type.Date', formatOptions: { pattern: 'dd.MM.yyyy' } }" />

After changing this to text it was working. Anyway the initial problem was the order of the definitions

Thanks guys


Solution

  • It looks like your imports are off. Try fixing it like this (pay attention to the define([]) block)

    sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel",
    "sap/m/MessageToast"
    ], function (Controller, JSONModel, MessageToast) {
    "use strict";
    return Controller.extend("sap.ui.bookedTimes.wt.controller.Main", {       
        onInit   : function () {            
            var jModel = this.getOwnerComponent().getModel("zCatsTestJ");
            var that = this;    
            jModel.attachRequestCompleted(function() {                  
                console.log(that.getView());
                var oViewModel= new JSONModel({workdate: "test"});
                console.log("before");
                that.getView().setModel(oViewModel, "view");
                console.log("after");
                console.log(that.getView().getModel("view"));    
            });
        },
    });
    });
    

    Now you should have JSONModel correctly imported and shouldn't see any errors.