清单中定义的访问模型

问题描述 投票:-1回答:4

在manifest.json中使用单个模型定义

"models": {
    "i18n": {
        "type": "sap.ui.model.resource.ResourceModel",
        "settings": {
            "bundleName": "....i18n.i18n"
        }
    },
    "": {
        "dataSource": "mainService"
    }
}

我可以从XML绑定到模型,它的工作

<List items="{path: '/myOneSet'}"> ... </List>
<List items="{path: '/myTwoSet'}"> ... </List>

但我无法从代码中访问它

this.getView().getModel().getProperty('/myOneSet') 

要么

this.getView().getModel().getProperty('/myOneSet/>param')

不行。怎么访问?

sapui5
4个回答
3
投票

如果mainService是OData服务,那么您的模型是ODataModel。您可以通过API中描述的方法访问数据,例如:read()

this.getView().getModel().read('/myOneSet', {
    success: function(oData, response) {
        // do sg. with oData
    }
});

它的大多数数据访问方法都有第二个带有成功函数回调的参数对象。在从数据源成功检索数据后,将与给定路径上的数据异步调用此函数。大多数ODataModel API方法都适用于回调。你可以阅读更多关于回调here的信息。


0
投票

如果您在manifest.json文件中定义了模型,则可以通过Component访问它。如果你通过模板创建项目,那么你有一个BaseController.js文件,它是你的其他控制器的祖先,并且有一个函数getComponentModel。所以,我建议你在你的控制器中尝试这个代码:

this.getComponentModel().getProperty('/myOneSet') 

0
投票

由于您尝试访问的属性名称,我假设您使用的是ODataModel。那是对的吗?

如果这是真的你必须考虑一些事情...... ODataModel.getProperty()不会触发请求。相反,它将返回已经可用的内容。如果要触发请求,则应执行ODataModel.read()。如果要从ODataModel获取加载的数据,那么您还应该知道何时加载了数据。这可以通过在执行绑定时附加更改事件处理程序来实现。

我没有描述如何访问ODataModel的数据,而是为您编写了一些小jsbin example(或参见下文)。只需检查更改事件处理程序。有多种方法可以访问数据。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>SAPUI5 single file template | nabisoft</title>
        <script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
            id="sap-ui-bootstrap"
            data-sap-ui-theme="sap_bluecrystal"
            data-sap-ui-libs="sap.m"
            data-sap-ui-bindingSyntax="complex"
            data-sap-ui-compatVersion="edge"
            data-sap-ui-preload="async"></script>
            <!-- use "sync" or change the code below if you have issues -->
 
        <!-- XMLView -->
        <script id="myXmlView" type="ui5/xmlview">
            <mvc:View
                controllerName="MyController"
                xmlns="sap.m"
                xmlns:core="sap.ui.core"
                xmlns:mvc="sap.ui.core.mvc">
 
                <Table
                    id="myTable"
                    growing="true"
                    growingThreshold="10"
                    growingScrollToLoad="true"
                    busyIndicatorDelay="0">
                    <headerToolbar>
                        <Toolbar>
                            <Title text="Orders of ALFKI"/>
                            <ToolbarSpacer/>
                        </Toolbar>
                    </headerToolbar>
                    <columns>
                        <Column>
                            <Text text="OrderID"/>
                        </Column>
                        <Column>
                            <Text text="Order Date"/>
                        </Column>
                        <Column>
                            <Text text="To Name"/>
                        </Column>
                        <Column>
                            <Text text="Ship City"/>
                        </Column>
                    </columns>
                    <items>
                        <!-- filled via bindItems() in controller -->
                    </items>
                </Table>
 
            </mvc:View>
        </script>
 
        <!-- XML Fragment -->
        <script id="myXMLFragment" type="ui5/fragment">
            <core:FragmentDefinition
                xmlns="sap.m"
                xmlns:core="sap.ui.core">
                <ColumnListItem type="Active">
                    <cells>
                        <ObjectIdentifier title="{OrderID}"/>
 
                        <Text
                            text="{
                                path:'OrderDate',
                                type:'sap.ui.model.type.Date',
                                formatOptions: { style: 'medium', strictParsing: true}
                            }"/>
 
                        <Text text="{ShipName}"/>
 
                        <Text text="{ShipCity}"/>
 
                    </cells>
                </ColumnListItem>
            </core:FragmentDefinition>
        </script>
 
        <script>
            sap.ui.getCore().attachInit(function () {
                "use strict";
 
                //### Controller ###
                sap.ui.define([
                    "sap/ui/core/mvc/Controller",
                    "sap/ui/model/odata/v2/ODataModel"
                ], function (Controller, ODataModel) {
                    "use strict";
 
                    return Controller.extend("MyController", {
                        onInit : function () {
                            this.getView().setModel(
                                new ODataModel("https://cors-anywhere.herokuapp.com/services.odata.org/V2/Northwind/Northwind.svc/", {
                                    json : true,
                                    useBatch : false
                                })
                            );
 
                            var sPath = "/Customers('ALFKI')/Orders";
                            var oTable = this.byId("myTable");
                            var oTemplate =  sap.ui.xmlfragment({
                                fragmentContent : jQuery("#myXMLFragment").html()
                            });                              
                            
                            oTable.bindItems({
                              path : sPath,
                              template : oTemplate,
                              templateShareable : false,
                              sorter : null, 
                              filters : null,
                              events : {
                                change : function (oEvent) {
                                  var oModel, oListBnd, aContexts, i, oObj;
                                  
                                  console.log("1. Example: ODataModel.getProperty() does not trigger a request (but here we have the data already)!");
                                  oModel = this.getView().getModel();
                                  console.dir( oModel.getProperty("/Orders(10643)"));  //OK
                                  console.dir( oModel.getProperty("/Orders") );       //Not OK
                                  console.dir( oModel.getData("/") );             //DANGER: Could contain different entities!!!!!
                                  
                                  console.log("2. Example: Accessing the Data via ListBinding");
                                  oListBnd = oTable.getBinding("items");      // get the ListBinding
                                  //oListBnd = oEvent.getSource();                // this works here inside the change handler
                                  aContexts = oListBnd.getCurrentContexts();  // the the contexts
                                  for(i=0; i<aContexts.length; i++){
                                    oObj = aContexts[i].getObject();   // access the items/objects
                                    console.dir( oObj ); 
                                  }                                  
                                  
                                }.bind(this)
                              }
                            });
                          
                        }
                    });
                });
 
                //### THE APP: place the XMLView somewhere into DOM ###
                sap.ui.xmlview({
                    viewContent : jQuery("#myXmlView").html()
                }).placeAt("content");
 
            });
        </script>
 
    </head>
 
    <body class="sapUiBody">
        <div id="content"></div>
    </body>
</html>

-1
投票

嗨,请声明这是显而易见的

"models": {
	"createMod": {
				"preload": true,
				"dataSource": "CreateService",
				"settings": {
				}
}

并尝试使用控制器访问。 var oModelCheck = that.getOwnerComponent()。getModel(“createMod”);

© www.soinside.com 2019 - 2024. All rights reserved.