如何在OpenUI5中使用聚合绑定时获取对象密钥

时间:2018-03-06 06:36:59

标签: sapui5

例如,我使用以下数据初始化了sap.ui.model.json.JSONModel

{
    "data": {
        "something": {"a": "blah", "b": "blah", "c": "blah"},
        "another": {"a": "blah", "b": "blah", "c": "blah"}
    }
}

我在XMLView中有一个sap.m.List,如下所示:

<List items="{/data}">
    <items>
        <ObjectListItem title="{a}" intro="">
            <firstStatus>
                <ObjectStatus title="{b}" text="{c}"/>
            </firstStatus>
        </ObjectListItem>
    </items>
</List>

现在我希望这两个intro的{​​{1}}属性为&#34;&#34;和另一个&#34;,但除了将数据更改为

之外我无法找到方法
ObjectListItem

然后绑定{ "data": { "something": {"key": "something", "a": "blah", "b": "blah", "c": "blah"}, "another": {"key": "another", "a": "blah", "b": "blah", "c": "blah"} } } ,这似乎很愚蠢(它引入了冗余并浪费了服务器带宽)。

那么,任何想法?

1 个答案:

答案 0 :(得分:1)

使用自定义工厂函数,从上下文路径获取所需的密钥:

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv='X-UA-Compatible' content='IE=edge'>
		<meta charset="utf-8">

		<title>MVC with XmlView</title>

		<!-- Load UI5, select "blue crystal" theme and the "sap.m" control library -->
		<script id='sap-ui-bootstrap'
				src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js'
			data-sap-ui-theme='sap_bluecrystal'
			data-sap-ui-libs='sap.m'
			data-sap-ui-xx-bindingSyntax='complex'></script>

		<script id="view1" type="sapui5/xmlview">
		    <mvc:View
				controllerName="my.own.controller"
				xmlns:l="sap.ui.layout"
				xmlns:core="sap.ui.core"
				xmlns:mvc="sap.ui.core.mvc"
				xmlns:f="sap.ui.layout.form"
				xmlns="sap.m">
			    <List items="{path:'/data', factory: '.myFactory'}">
          </List>
			</mvc:View>
	</script>


		<script>
			// define a new (simple) Controller type
			sap.ui.controller("my.own.controller", {
			  myFactory: function(sId, oContext){
			    var keyPath = oContext.getPath();
			    console.log(oContext);
			    
			    var oObjectListItem = new sap.m.ObjectListItem({
			      title: {path: keyPath + '/a'},
			      intro: keyPath.split('/')[keyPath.split('/').length - 1],
			      firstStatus: [
			        new sap.m.ObjectStatus({
			          title: {path: keyPath + '/b'},
			          text: {path: keyPath + '/c'}
			        })
			      ]
			    });
			    
			    return oObjectListItem;
			  }
			});

			// instantiate the View
			var myView = sap.ui.xmlview({viewContent:jQuery('#view1').html()}); // accessing the HTML inside the script tag above

			// create some dummy JSON data
			var data = {
          "data": {
              "something": {"a": "blah", "b": "blah", "c": "blah"},
              "another": {"a": "blah", "b": "blah", "c": "blah"}
          }
      }
			
			// create a Model and assign it to the View
			var oModel = new sap.ui.model.json.JSONModel();
			oModel.setData(data);
			myView.setModel(oModel);
			
			
			// put the View onto the screen
			myView.placeAt('content');

		</script>
	
	</head>
	<body id='content' class='sapUiBody'>
	</body>
</html>