Skip to content

Commit

Permalink
Added everything for Step 15: Aggregation Binding Using a Factory Fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
wridgeu committed May 20, 2020
1 parent eb30253 commit 3b8f0b0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 24 deletions.
31 changes: 30 additions & 1 deletion webapp/controller/Home.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ sap.ui.define(
[
"com/mrb/UI5-Data-Binding/controller/BaseController",
"sap/ui/model/json/JSONModel",
"sap/m/ObjectAttribute"
],
function (BaseController, JSONModel) {
function (BaseController, JSONModel, ObjectAttribute) {
"use strict";

return BaseController.extend("com.mrb.UI5-Data-Binding.controller.Home", {
Expand Down Expand Up @@ -48,6 +49,34 @@ sap.ui.define(
var oProductDetailPanel = this.byId("productDetailsPanel");
oProductDetailPanel.bindElement({ path: sPath, model: "products" });
},
productListFactory: function (sId, oContext) {
var oUIControl;

// Decide based on the data which dependent to clone
if (
oContext.getProperty("UnitsInStock") === 0 &&
oContext.getProperty("Discontinued")
) {
// The item is discontinued, so use a StandardListItem
oUIControl = this.byId("productSimple").clone(sId);
} else {
// The item is available, so we will create an ObjectListItem
oUIControl = this.byId("productExtended").clone(sId);

// The item is temporarily out of stock, so we will add a status
if (oContext.getProperty("UnitsInStock") < 1) {
oUIControl.addAttribute(
new ObjectAttribute({
text: {
path: "i18n>outOfStock",
},
})
);
}
}

return oUIControl;
},
});
}
);
1 change: 1 addition & 0 deletions webapp/i18n/i18n.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ QuantityPerUnit=Quantity per Unit
UnitPrice=Unit Price
UnitsInStock=Number of Units in Stock
Discontinued=Discontinued
outOfStock=Out of Stock

# Invoice List
invoiceListTitle=Invoices
Expand Down
1 change: 1 addition & 0 deletions webapp/i18n/i18n_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ QuantityPerUnit=Menge pro Einheit
UnitPrice=Preis der Einheit
UnitsInStock=Lagerbestand
Discontinued=Eingestellt
outOfStock=Nicht vorr\u00e4tig

# Invoice List
invoiceListTitle=Rechnungen
Expand Down
1 change: 1 addition & 0 deletions webapp/i18n/i18n_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ QuantityPerUnit=Quantity per Unit
UnitPrice=Unit Price
UnitsInStock=Number of Units in Stock
Discontinued=Discontinued
outOfStock=Out of Stock

# Invoice List
invoiceListTitle=Invoices
Expand Down
32 changes: 9 additions & 23 deletions webapp/view/Home.view.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<mvc:View controllerName="com.mrb.UI5-Data-Binding.controller.Home" displayBlock="true"
xmlns="sap.m"
xmlns:l="sap.ui.layout"
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc">
<Page titleAlignment="Center" title="{i18n>title}">
<Panel headerText="{i18n>panel1HeaderText}" class="sapUiResponsiveMargin" width="auto">
Expand Down Expand Up @@ -40,29 +41,14 @@
</Panel>
<Panel headerText="{i18n>panel3HeaderText}" class="sapUiResponsiveMargin" width="auto">
<content>
<List headerText="{i18n>productListTitle}" items="{products>/Products}">
<items>
<ObjectListItem press=".onItemSelected" type="Active" title="{products>ProductName}" number="{
parts: [
{path: 'products>UnitPrice'},
{path: '/currencyCode'}
],
type: 'sap.ui.model.type.Currency',
formatOptions: { showMeasure: false }
}" numberUnit="{/currencyCode}" numberState="{= ${products>UnitPrice} > ${/priceThreshold} ? 'Error' : 'Success' }">
<attributes>
<ObjectAttribute text="{products>QuantityPerUnit}"/>
<ObjectAttribute title="{i18n>stockValue}" text="{
parts: [
{path: 'products>UnitPrice'},
{path: 'products>UnitsInStock'},
{path: '/currencyCode'}
],
formatter: '.formatter.formatStockValue'
}"/>
</attributes>
</ObjectListItem>
</items>
<List id="ProductList" headerText="{i18n>productListTitle}" items="{
path: 'products>/Products',
factory: '.productListFactory'
}">
<dependents>
<core:Fragment fragmentName="com.mrb.UI5-Data-Binding.view.ProductSimple" type="XML"/>
<core:Fragment fragmentName="com.mrb.UI5-Data-Binding.view.ProductExtended" type="XML"/>
</dependents>
</List>
</content>
</Panel>
Expand Down
14 changes: 14 additions & 0 deletions webapp/view/ProductExtended.fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<core:FragmentDefinition xmlns="sap.m"
xmlns:core="sap.ui.core">
<ObjectListItem id="productExtended" title="{products>ProductName} ({products>QuantityPerUnit})" number="{
parts: [
{path: 'products>UnitPrice'},
{path: '/currencyCode'}
],
type: 'sap.ui.model.type.Currency',
formatOptions : {
showMeasure : false
}
}" type="Active" numberUnit="{/currencyCode}" press=".onItemSelected">
</ObjectListItem>
</core:FragmentDefinition>
5 changes: 5 additions & 0 deletions webapp/view/ProductSimple.fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<core:FragmentDefinition xmlns="sap.m"
xmlns:core="sap.ui.core">
<StandardListItem id="productSimple" icon="sap-icon://warning" title="{products>ProductName} ({products>QuantityPerUnit})" info="{i18n>Discontinued}" type="Active" infoState="Error" press=".onItemSelected">
</StandardListItem>
</core:FragmentDefinition>

0 comments on commit 3b8f0b0

Please sign in to comment.