-1

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

zerocool985
  • 3
  • 1
  • 4
  • the order you are importing your modules is not the same as the order of the parameters in your callback function. Change it like this and try again `], function (Controller, MessageToast, JSONModel) {` – Rafael López Martínez Jul 24 '18 at 12:06
  • now the `console.log("before")` is also not showing – zerocool985 Jul 24 '18 at 12:17
  • @RafaelLópezMartínez that is not correct. - the context of `this` doesn't affect the usage of the definitions in `sap.ui.define([...])` – Kyle Jul 24 '18 at 18:47
  • @zerocool985 You need to correct the order of your definitions. They need to match. So if you define `sap/ui/core/mvc/Controller, sap/ui/model/json/JSONModel` you need to have `function(Controller, JSONModel...` - after that, check to make sure that your JSON Model is actually completing a request, and that the request is completed AFTER you have attach your event handler. If the JSON Model isn't completing a request or completing it before you attach your event, your code won't execute – Kyle Jul 24 '18 at 19:07
  • @Kyle I have thrown out `'"jquery.sap.global"` and `"sap/m/MessageToast"`. When I `console.log(oViewModel)` I can see the constructor with "test" data in it. Still after `that.getView().setModel(oViewModel, "view")` nothing happens anymore – zerocool985 Jul 24 '18 at 19:32
  • @zerocool985 Can you edit your question with your latest code so I can take a closer look? – Kyle Jul 24 '18 at 19:33

1 Answers1

0

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.

Matthew C Reddy
  • 322
  • 1
  • 9