Skip to content

Instantly share code, notes, and snippets.

@ricma
Last active August 29, 2015 14:05
Show Gist options
  • Save ricma/c1517e10b83ac543a2dc to your computer and use it in GitHub Desktop.
Save ricma/c1517e10b83ac543a2dc to your computer and use it in GitHub Desktop.
Demonstrate issue with binding in ODataModels in sapui5/openui5: Binding aggregations to properties, see http://scn.sap.com/thread/3613164
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='X-UA-Compatible' content='IE=edge' />
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>
<script src='https://openui5.hana.ondemand.com/resources/sap-ui-core.js'
id='sap-ui-bootstrap'
data-sap-ui-libs='sap.ui.commons'
data-sap-ui-theme='sap_goldreflection'
type='text/javascript'>
</script>
<script type='text/javascript'>
var sUrl = "http://services.odata.org/Northwind/Northwind.svc";
//sUrl = "http://services.odata.org/V3/OData/OData.svc";
// They yield a
// 'NetworkError: 501 Not Implemented -
// http://services.odata.org/....svc/$metadata'
// in FireFox, use Chrome with 'chrome --disable-web-security'
var oOdataModel = new sap.ui.model.odata.ODataModel(sUrl, /* bJSON */ true);
var oJsonModel = new sap.ui.model.json.JSONModel({
Categories: [
{CategoryID: 1, CategoryName: "Beverages"},
{CategoryID: 2, CategoryName: "Food"},
{CategoryID: 3, CategoryName: "Assets"},
{CategoryID: 4, CategoryName: "Animals"}]});
function setupExample(oModel) {
var oBox = new sap.ui.commons.DropdownBox({
items: {
path: "/Categories",
template: new sap.ui.core.ListItem({
text: "{CategoryName}",
key: "{CategoryID}"})}}).
setModel(oModel).
placeAt("content");
// FIXME: The above does not seem to work when using
// path: "Categories"
// and `.bindElement("/")' for the the oData model ... why?
var oText = new sap.ui.commons.TextView({
text: {
path: "Categories",
formatter: function(aCats) {
return "There are " + (aCats || []).length + " elements in the box";
}}}).
setModel(oModel).
bindElement("/").
placeAt("content");
}
// Problem: Is the text on the rhs of the box rendered correctly,
// i.e. does it show the number of entries?
// Note: It works for a plain JSON model! Can we force the
// odata model to load aggregations as well?
setupExample(oOdataModel);
setupExample(oJsonModel);
</script>
</head>
<body class="sapUiBody">
<div id="content"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment