简体   繁体   English

在根视图的onInit()中设置模型时出错-没有错误/没有日志

[英]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). 我试图在Main.view.xml (根视图)上设置一个new JSONModel But it seems like it is stoping at .setModel() . 但似乎它停止在.setModel() The console.log("after") is not logging. console.log("after")未记录。

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 : 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? 根视图的onInit()是否存在问题?

Update: I should have added the part of the xml.view. 更新:我应该已经添加了xml.view的一部分。 I changed the view name to "view1" and everything from the controller was logged. 我将视图名称更改为“ view1”,并记录了控制器的所有内容。 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 多谢你们

It looks like your imports are off. 您的进口货似乎已关闭。 Try fixing it like this (pay attention to the define([]) block) 尝试像这样修复它(注意define([])块)

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. 现在,您应该正确导入了JSONModel,并且不会看到任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM