Model view controller 无法从sapUI5中的core访问模型

Model view controller 无法从sapUI5中的core访问模型,model-view-controller,sapui5,Model View Controller,Sapui5,有关问题的后续行动 无论出于何种原因,当通过sap.ui.getCore().setModel()设置模型时,我无法在xml视图中访问模型。如果我在this.getView()上设置它,我一点问题都没有 我的视图XML <mvc:View controllerName="ca.toronto.rcsmls.webapp.controller.Login" xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:l="sap.ui.layout"&g

有关问题的后续行动

无论出于何种原因,当通过sap.ui.getCore().setModel()设置模型时,我无法在xml视图中访问模型。如果我在this.getView()上设置它,我一点问题都没有

我的视图XML

<mvc:View controllerName="ca.toronto.rcsmls.webapp.controller.Login"
xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:l="sap.ui.layout">

<Page title="{i18n>loginPageTitle}">
    <content>

        <Panel id="loginPanel" busyIndicatorDelay="0"
            headerText="{i18n>loginPanelTitle}" class="sapUiResponsiveMargin loginPanel"
            width="auto">
            <l:VerticalLayout width="100%">


                <Input type="Text" placeholder="{i18n>loginUidHolder}" value="{/mlsUser/uid}" />

                <Input type="Password" placeholder="{app>/Password}"
                    value="{/mlsUser/password}" />

                <Button text="{i18n>loginButtonText}" press="doLogin"
                    class="sapUiSmallMarginEnd customBold" width="100%" />


            </l:VerticalLayout>
        </Panel>

    </content>
</Page></mvc:View>
同样,如果我将模型设置为.getView().setModel()而不是getCore(),XML和控制器可以很好地协同工作。我还向我的index.html添加了数据sap ui xx bindingSyntax=“complex”,但这似乎没有什么不同。任何帮助都将不胜感激

编辑以包含更多信息

My Component.js

sap.ui.define([
   "sap/ui/core/UIComponent",
   "sap/ui/model/json/JSONModel",
], function (UIComponent, JSONModel) {
   "use strict";
   return UIComponent.extend("ca.toronto.rcsmls.webapp.Component", {
      metadata : {
          manifest: "json"
      },
      init : function () {
         // call the init function of the parent
         UIComponent.prototype.init.apply(this, arguments);
         // set data model
         var oData = {
            mlsUser : {
               uid : "",
               password : "",
            }
         };
         var oModel = new JSONModel(oData);
         this.setModel(oModel);

         // create the views based on the url/hash
         this.getRouter().initialize();
      }
   });
});
我的manifest.json

{
    "_version": "1.1.0",
    "sap.app": 
    {
        "_version": "1.1.0",
        "id": "ca.toronto.rcsmls",
        "type": "application",
        "i18n": "i18n/i18n.properties",
        "title": "{{appTitle}}",
        "description": "{{appDescription}}",
        "applicationVersion": 
        {
            "version": "1.0.0"
        },

        "ach": "CA-UI5-DOC"
    },

    "sap.ui": 
    {
        "_version": "1.1.0",
        "technology": "UI5",
        "deviceTypes": 
        {
            "desktop": true,
            "tablet": true,
            "phone": true
        },

        "supportedThemes": 
        [
            "sap_bluecrystal"
        ]
    },

    "sap.ui5": 
    {
        "_version": "1.1.0",
        "rootView": "ca.toronto.rcsmls.webapp.view.App",
        "dependencies": 
        {
            "minUI5Version": "1.30",
            "libs": 
            {
                "sap.m": 
                {

                }
            }
        },

        "config": 
        {
            "authenticationService": "http://172.21.226.138:9080/RcsMlsSvc/jaxrs/user/authenticate/",
            "assignedWorkService": "http://172.21.226.138:9080/RcsMlsSvc/jaxrs/mls/searchAssignedWork"
        },

        "models": 
        {
            "i18n": 
            {
                "type": "sap.ui.model.resource.ResourceModel",
                "settings": 
                {
                    "bundleName": "ca.toronto.rcsmls.webapp.i18n.i18n"
                }
            }
        },

        "routing": 
        {
            "config": 
            {
                "routerClass": "sap.m.routing.Router",
                "viewType": "XML",
                "viewPath": "ca.toronto.rcsmls.webapp.view",
                "controlId": "root",
                "controlAggregation": "pages"
            },

            "routes": 
            [
                {
                    "pattern": "",
                    "name": "login",
                    "target": "login"
                },
                {
                    "pattern": "work",
                    "name": "work",
                    "target": "work"
                }
            ],

            "targets": {
                "login": {
                    "viewName": "Login"
                },
                "work": {
                    "viewName": "Work"
                }
            }
        },

        "resources": 
        {
            "css": 
            [
                {
                    "uri": "css/style.css"
                }
            ]
        }
    }
}
模型app.json

{
    "BaseURL": "https://smp-pNNNNNNtrial.hanatrial.ondemand.com",
    "ES1Root": "https://sapes1.sapdevcenter.com",
    "AppName": "qmacro.myfirst",
    "Username": "yourusername",
    "Password": "yourpassword"
}

我发现了一个核心绑定工作的例子。这是一个简单得多的应用程序。我仍在试图找出这个项目和我的项目之间的区别。我在上面的视图中,只有密码字段的占位符在使用你的应用程序模型。如果此占位符未正确填充,则我猜无法加载json文件,或者内容与您在视图中为密码字段的占位符属性使用的绑定路径不匹配。要了解更多信息,请同时发布json文件的内容。它应该是这样的:

{ "Password" : "Enter your password", ... }
因此,根据视图中的绑定,在数据对象的根级别必须有“Password”属性

下面是一个可以帮助您的运行示例。正如您所看到的,它的工作方式很有魅力,因此您可以轻松地将命名模型放到核心上,并在视图中引用它


SAPUI5单文件模板| nabisoft
sap.ui.getCore().attachInit(函数(){
“严格使用”;
//###控制器###
sap.ui.controller(“MyController”{
onInit:function(){
var oData,oModel;
//1.应用程序模型仅用于视图中的占位符字段
小田={
密码:“输入您的密码”
};
oModel=new sap.ui.model.json.JSONModel(oData);
setModel(oModel,“app”);
//2.视图中也使用默认模型
小田={
mlsUser:{},
登录:“立即登录”
};
oModel=new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel);
//3.我们之所以需要它,是因为它具有相对约束力
//登录按钮的文本属性的
此.getView().bindElement(“/”);
}
});
//###应用程序:将XMLView放在DOM中的某个位置###
sap.ui.xmlview({
viewContent:jQuery(“#myXmlView”).html()
}).placeAt(“内容”);
});

我也经历过同样的行为。 如果我创建一个简单的单文件应用程序,没有任何复杂的UI元素,那么基于核心的绑定就像一个魅力

如果我创建一个复杂的容器,比如一个带外壳的应用程序,这种绑定将不再有效。这些容器似乎在视图中隐藏了全局模型

作为解决方法,我使用了以下代码段:

this.getView().setModel(sap.ui.getCore().getModel(modelName), "modelName");
甚至可以将模型直接绑定到要使用的控件

如果您必须在多个视图/控件中使用全局模型,那么它们都不是最佳的解决方案,但这对我来说是可行的。

sap.ui.getCore()
this.getView()
返回的对象不同,我认为这显然是不可行的原因

您正试图从另一个对象(核心)获取对象(模型),尽管所需的模型已绑定到另一个对象(视图)

就像我有两个颜色的盒子(一个红色,一个蓝色),我试着从蓝色的盒子里得到红色

这是一个类似的问题,但其中的原因取决于UI5框架中视图的id处理:

您可以看到核心和视图不返回相同的对象

还可以在openui5网站上查看数据绑定部分:

在components.js中创建模型:

var oModel= new sap.ui.model.json.JSONModel;
oModel.loadData("webapp/controller/app.json");
this.setModel(oModel, "app");
获取模型: 这将创建对已在Components.js中创建的模型的引用

var oModel= this.getView().getModel("app");
使用“{modelName(app)>desiredProperty}”引用模型


你能发布你的json内容吗


希望这对我有帮助

我是通过一个类似的问题意识到这个问题的

默认情况下,UI5组件不会从其环境继承模型和绑定上下文(放置它们的
ComponentContainer
)。这样做是为了隔离

通过为容器设置属性
propagateModel:true
,可以更改该默认行为:

new sap.ui.core.ComponentContainer({
  name: "ca.toronto.rcsmls.webapp.Component",
  propagateModel: true
}).placeAt("content");
propagateModel
的文档可以在中找到,也可以在UI5中找到(后一页中的相应段落是最近才添加的,在您提出问题时不可用)

但是,当模型在组件范围内创建并应在组件内使用时,则无需将其添加到核心。只需将其指定给组件,即可在该组件内部的视图之间共享它
<Input type="Password" placeholder="{app>Password}"
                    value="{app>mlsUser/password}" />
new sap.ui.core.ComponentContainer({
  name: "ca.toronto.rcsmls.webapp.Component",
  propagateModel: true
}).placeAt("content");
onInit : function() {
    this.getOwnerComponent().setModel(
        new sap.ui.model.json.JSONModel(
            "webapp/controller/app.json"), "app");
}
init : function() {
    // create and set model to make it available to all views
    this.setModel(
        new sap.ui.model.json.JSONModel(
            "webapp/controller/app.json"), "app");

    // never forget to call init of the base class
    UIComponent.init.apply(this, arguments);
}