Data Binding Tutorial - 15 Aggregation Binding Using a Factory Function

ํ™์ข…ํ™”ยท2021๋…„ 11์›” 2์ผ
0

databinding

๋ชฉ๋ก ๋ณด๊ธฐ
15/16

๐Ÿ“Œ ๊ฐœ์š”

single template control์„ ํ•˜๋“œ ์ฝ”๋”ฉํ•˜๋Š” ๋Œ€์‹  factory function์„ ์‚ฌ์šฉํ•˜์—ฌ runtime์— ์ˆ˜์‹ ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค๋ฅธ control์„ ์ƒ์„ฑํ•œ๋‹ค.
์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ํ›จ์”ฌ ๋” ์œ ์—ฐํ•˜๋ฉฐ ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ์ด์งˆ์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ ์˜ˆ์ œ

  • view.xml
      <Panel headerText="{i18n>panel4HeaderText}" class="sapUiResponsiveMargin" width="auto">
        <List id="ProductList" headerText="{i18n>productListTitle}" items="{
									path: 'products>/Products',
									factory: '.productListFactory'}">
          <dependents>
            <core:Fragment fragmentName="databinding.view.ProductSimple" type="XML"/>
            <core:Fragment fragmentName="databinding.view.ProductExtended" type="XML"/>
          </dependents>
        </List>
      </Panel>

์ด์ „์— product list๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ List XML element๋Š” ์ด์ œ fragment๋กœ ์ถ•์†Œ๋˜์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด list๋Š” ๋น„์–ด ์žˆ๋‹ค. ์ด list๋ฅผ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š” factory function์ด ์—†์œผ๋ฉด ์ด list๋Š” ํ•ญ์ƒ ๋น„์–ด ์žˆ๋‹ค.

  • controller.js
'sap/m/ObjectAttribute'], function (..., ObjectAttribute) {
	"use strict";
	...
	ProductListFactory: function(sId, oContext) {
		let oUIControl;
		
		// clone์— ์˜์กดํ•˜๋Š” data๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฒฐ์ •
		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;
		
	}
}

factory function์€ ์ด์ „ ๋‹จ๊ณ„์—์„œ ์ •์˜ํ•œ XML template๊ณผ ์œ ์‚ฌํ•œ ๊ด€๋ จ binding context์— ๋Œ€ํ•œ control์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์ด factory function์ด ๋ฐ˜ํ™˜ํ•˜๋Š” control type์€ sap.m.List ๊ฐ์ฒด์˜ ํ•ญ๋ชฉ aggregation์— ์ ํ•ฉํ•ด์•ผ ํ•œ๋‹ค.
์ด ๊ฒฝ์šฐ ์ƒ์„ฑํ•  ํ•ญ๋ชฉ์˜ context์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ StandardListItem ๋˜๋Š” ObjectListItem์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

ํ˜„์žฌ ์žฌ๊ณ ์ˆ˜์ค€๊ณผ ์ œํ’ˆ์˜ ๋‹จ์ข… ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์—ฌ ์–ด๋–ค ์ข…๋ฅ˜์˜ ์ œ์–ด์žฅ์น˜๋ฅผ ๋ฐ˜ํ’ˆํ• ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค. ๋‘ ์˜ต์…˜ ๋ชจ๋‘ XML์„ ๋†“๊ณ 
๋กœ๋“œํ•˜์—ฌ ๋ทฐ ๋กœ์ง์„ ์„ ์–ธ์ ์œผ๋กœ ์ •์˜ํ•˜๊ณ  ํ˜„์žฌ ์ปจํŠธ๋กค๋Ÿฌ์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. ์žฌ๊ณ  ๋ ˆ๋ฒจ์ด 0์ด๊ณ  ๋‹จ์ข…๋œ ๊ฒฝ์šฐ ProductSimple(sap.m.StandardListItem),
๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ProductExtended(sap.m.ObjectListItem)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

XML ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ๊ฐ ๊ฒฝ์šฐ์— ํ•œ ๋ฒˆ๋งŒ ๋กœ๋“œํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ปจํŠธ๋กค๋Ÿฌ์— helper varaible์„ ์ €์žฅํ•˜๊ณ  ํ•œ ๋ฒˆ๋งŒ ๋กœ๋“œํ•˜์—ฌ
Singleton์„ ๋งŒ๋“ ๋‹ค. list์˜ ๊ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์ปจํŠธ๋กค๋Ÿฌ์— ์ €์žฅ๋œ ํ•ด๋‹น control์„ ๋ณต์ œํ•œ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” list ํ•ญ๋ชฉ์˜ context์— bindingํ•  ์ˆ˜ ์žˆ๋Š” control์˜ ์ƒˆ ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ ๋‹ค.
์ฐธ๊ณ : facotry function์—์„œ๋Š” ์ƒ์„ฑํ•œ control์˜ life cycle์— ๋Œ€ํ•œ ์ฑ…์ž„์ด ์žˆ๋‹ค.

๋‹จ์ข…๋œ ์ œํ’ˆ์€ ์•„์ง€๋งŒ ์žฌ๊ณ ๊ฐ€ 0์ธ ๊ฒฝ์šฐ ์ผ์‹œ์ ์œผ๋กœ ํ’ˆ์ ˆ๋œ๋‹ค. ์ด ๊ฒฝ์šฐ Javascript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ control์— ์žฌ๊ณ  ์—†์Œ ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋‹จ์ผ ObjectAttribute๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
XML view๋‚˜ fragment์—์„œ ์„ ์–ธ์  ์ •์˜์™€ ์œ ์‚ฌํ•˜๊ฒŒ data binding syntax์„ ์‚ฌ์šฉํ•˜์—ฌ ์†์„ฑ์„ ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด ๊ฒฝ์šฐ ํ…์ŠคํŠธ๋ฅผ ๋ฆฌ์†Œ์Šค ๋ฒˆ๋“ค์˜ ์†์„ฑ์— ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
์†์„ฑ์€ ๋ชฉ๋ก ํ•ญ๋ชฉ์˜ ํ•˜์œ„ ํ•ญ๋ชฉ์ด๋ฏ€๋กœ ํ• ๋‹น๋œ ๋ชจ๋“  ๋ชจ๋ธ๊ณผ ํ˜„์žฌ ๋ฐ”์ธ๋”ฉ ์ปจํ…์ŠคํŠธ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ProductSimple.fragement.xml
    ์ด XML์€ ์žฌ๊ณ  ์ˆ˜์ค€์ด 0์ด๋ฉด์„œ ์ œํ’ˆ๋„ ๋‹จ์ข…๋œ ๊ฒฝ์šฐ ์‚ฌ์šฉ๋˜๋Š” StandardListItem์„ ์ •์˜ํ•œ๋‹ค.
    ์ด๊ฒƒ์€ info ํ”„๋กœํผํ‹ฐ์—์„œ ๊ฒฝ๊ณ  ์•„์ด์ฝ˜๊ณผ Product Discontinued ๋ฉ”์‹œ์ง€๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ด๋‹ค.
<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>
  • ProductExtended.fragment.xml
    ํ™•์žฅ๋œ ์˜ˆ์—์„œ๋Š” ์ œํ’ˆ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ObjectListItem์„ ๋งŒ๋“ ๋‹ค.
    ํ”„๋กœํผํ‹ฐ๋Š” ํ˜„์žฌ data binding context์˜ ํ•„๋“œ์— ๋ฐ”์ธ๋”ฉ๋˜๋ฏ€๋กœ ํ• ๋‹น๋œ ์ปจํŠธ๋กค๋Ÿฌ์— ์ •์˜๋œ ํƒ€์ž…, ํฌ๋งทํ„ฐ ๋ฐ ๋ชจ๋“  ์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    ๊ทธ๋Ÿฌ๋‚˜ ๋” ๋ณต์žกํ•œ ๋กœ์ง์€ XML์—์„œ ์„ ์–ธ์ ์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์žฌ๊ณ  ์ˆ˜์ค€์ด 0์ผ ๋•Œ Javascript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปจํŠธ๋กค๋Ÿฌ์— ์žฌ๊ณ  ์—†์Œ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋‹จ์ผ ObjectAttribute๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
<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>
  • i18n.properties
# Product Details
outOfStock=Out of Stock

๐Ÿ‘‹ databinding tutorial~

sapui5์—์„œ ์ œ๊ณตํ•˜๋Š” databinding์—๋Œ€ํ•ด ํฐ ํ‹€์˜ ๊ฐœ๋…๋งŒ ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค. ์ถ”ํ›„ databinding์— ๋” ๋”ฅํ•˜๊ฒŒ ์•Œ์•„๋ณด๊ณ  ๋งŽ์€ ์˜ˆ์ œ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๊ณต์œ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. databinding tutorial์—์„œ ๋‚ด์šฉ์ด ์ž˜๋ชป๋๊ฑฐ๋‚˜ ์„ค๋ช…์ด ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์€ ๋Œ“๊ธ€์„ ๋‹ฌ๊ฑฐ๋‚˜ ์ด๋ฉ”์ผ์„ ๋ณด๋‚ด์ฃผ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค~~ ์ตœ๋Œ€ํ•œ ๋นจ๋ฆฌ ๋ณด์™„ํ•ด์„œ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿ™๐Ÿ™๐Ÿ™

๐Ÿ”— ์ฐธ์กฐ

profile
coding everywhere

0๊ฐœ์˜ ๋Œ“๊ธ€