SAP How-to Guide Interface SAPUI5 Tutorial
Data Binding in SAPUI5
Applicable Releases: SAPUI5 v1.28 and higher
Version 1.0 July 2015
© Copyright 2015 SAP AG. All rights reserved.
All other product and service names mentioned are the trademarks of
No part of this publication may be reproduced or transmitted in any form
their respective companies. Data contained in this document serves
or for any purpose without the express permission of SAP AG. The
informational purposes only. National product specifications may vary.
information contained herein may be changed without prior notice.
The information in this document is proprietary to SAP. No part of this
Some software products marketed by SAP AG and its distributors
document may be reproduced, copied, or transmitted in any form or for
contain proprietary software components of other software vendors.
any purpose without the express prior written permission of SAP AG.
Microsoft, Windows, Excel, Outlook, and PowerPoint are ed
This document is a preliminary version and not subject to your license
trademarks of Microsoft Corporation.
agreement or any other agreement with SAP. This document contains
IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER,
only intended strategies, developments, and functionalities of the SAP® product and is not intended to be binding upon SAP to any particular course of business, product strategy, and/or development. Please note that this document is subject to change and may be changed by SAP at any time without notice.
OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS,
SAP assumes no responsibility for errors or omissions in this document.
HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex,
SAP does not warrant the accuracy or completeness of the information,
MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and
text, graphics, links, or other items contained within this material. This
Informix are trademarks or ed trademarks of IBM Corporation.
document is provided without a warranty of any kind, either express or
Linux is the ed trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or ed trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a ed trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are ed trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or ed trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or ed trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology.
implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. SAP shall have no liability for damages of any kind including without limitation direct, special, indirect, or consequential damages that may result from the use of these materials. This limitation shall not apply in cases of intent or gross negligence. The statutory liability for personal injury and defective products is not affected. SAP has no control over the information that you may access through the use of hot links contained in these materials and does not endorse your use of third-party Web pages nor provide any warranty whatsoever relating to third-party Web pages. SAP “How-to” Guides are intended to simplify the product implementtation. While specific product features and procedures typically are explained in a practical business context, it is not implied that those features and procedures are the only approach in solving a specific
Java is a ed trademark of Sun Microsystems, Inc.
business problem using SAP NetWeaver. Should you wish to receive
JavaScript is a ed trademark of Sun Microsystems, Inc., used
additional information, clarification or , please refer to SAP
under license for technology invented and implemented by Netscape.
Consulting.
SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP
Any software coding and/or code lines / strings (“Code”) included in this
BusinessObjects Explorer, StreamWork, and other SAP products and
documentation are only examples and are not intended to be used in a
services mentioned herein as well as their respective logos are
productive system environment. The Code is only intended better explain
trademarks or ed trademarks of SAP AG in and other
and visualize the syntax and phrasing rules of certain coding. SAP does
countries.
not warrant the correctness and completeness of the Code given herein,
Business Objects and the Business Objects logo, BusinessObjects,
and SAP shall not be liable for errors or damages caused by the usage of
Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other
the Code, except if such damages were caused by SAP intentionally or
Business Objects products and services mentioned herein as well as their
grossly negligent.
respective logos are trademarks or ed trademarks of Business
Disclaimer
Objects Software Ltd. Business Objects is an SAP company.
Some components of this product are based on Java™. Any code change
Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere,
in these components may cause unpredictable and severe malfunctions
and other Sybase products and services mentioned herein as well as their
and is therefore expressively prohibited, as is any decompilation of these
respective logos are trademarks or ed trademarks of Sybase, Inc.
components.
Sybase is an SAP company.
Any Java™ Source Code delivered with this product is only to be used by SAP’s Services and may not be modified or altered in any way.
Document History Document Version
Description
1.00
First official release of this guide
Typographic Conventions
Icons
Type Style
Description
Icon
Example Text
Words or characters quoted from the screen. These include field names, screen titles, pushbuttons labels, menu names, menu paths, and menu options. Cross-references to other documentation
Example text
Emphasized words or phrases in body text, graphic titles, and table titles
Example text
File and directory names and their paths, messages, names of variables and parameters, source text, and names of installation, upgrade and database tools.
Example text
entry texts. These are words or characters that you enter in the system exactly as they appear in the documentation.
<Example text>
Variable entry. Angle brackets indicate that you replace these words and characters with appropriate entries to make entries in the system.
EXAMPLE TEXT
Keys on the keyboard, for example, F2 or ENTER.
Description Caution Note or Important Example Recommendation or Tip
Table of Contents Step 1:
No Data Binding ...................................................................................................... 5
1.1. Preview .................................................................................................................... 5 1.2. ./index.html (New file) ............................................................................................... 6 Step 2:
Creating a Model Object ......................................................................................... 7
2.1. Preview .................................................................................................................... 7 2.2. ./index.html ............................................................................................................... 8 Warning! ................................................................................................................... 8 Step 3:
Making Model Object Data Available on the Interface .................................. 9
3.1. Preview .................................................................................................................... 9 3.2. ./index.html ............................................................................................................... 9 3.3. Explanation ............................................................................................................. 10 Important ................................................................................................................ 10 Step 4:
Two-Way Data Binding ......................................................................................... 11
4.1. Preview .................................................................................................................. 11 4.2. ./views/App.view.xml (New file in new folder)........................................................... 12 4.3. ./index.html ............................................................................................................. 12 4.4. Behaviour ............................................................................................................... 13 Explanation ............................................................................................................. 13 Important ................................................................................................................ 14 Step 5:
One-way Data Binding .......................................................................................... 16
5.1. Preview .................................................................................................................. 16 5.2. ./index.html ............................................................................................................. 16 5.3. Data Binding With Other Model Types..................................................................... 17 5.4. Background ............................................................................................................ 17 Important! ............................................................................................................... 17 Step 6:
Resource Models .................................................................................................. 18
6.1. Preview .................................................................................................................. 18 6.2. ./index.html ............................................................................................................. 19 6.3. ./i18n/i18n.properties (New file in new folder) .......................................................... 20 6.4. ./views/App.view.xml ............................................................................................... 20 Important ................................................................................................................ 20 Explanation ............................................................................................................. 21 Step 7:
(Optional) Resource Bundles and Multiple Languages ...................................... 22
7.1. Preview .................................................................................................................. 22 7.2. ./i18n/i18n_de.properties (New file) ......................................................................... 22 7.3. Behaviour ............................................................................................................... 22 Be Careful With Language Determination!............................................................... 23 Step 8:
Binding Paths – Accessing Properties in Hierarchically Structured Models ..... 24
8.1. Preview .................................................................................................................. 24 8.2. ./index.html ............................................................................................................. 25
8.3. ./views/App.view.xml ............................................................................................... 25 8.4. ./i18n/i18n.properties .............................................................................................. 26 8.5. ./i18n/i18n_de.properties (Optional) ........................................................................ 26 Explanation ............................................................................................................. 26 Step 9:
Formatting Model Property Values ...................................................................... 27
9.1. Preview .................................................................................................................. 27 9.2. ./controllers/App.controller.js (New file in new folder) ............................................... 28 Coding Style ........................................................................................................... 29 9.3. ./views/App.view.xml ............................................................................................... 30 9.4. ./i18n/i18n.properties .............................................................................................. 30 9.5. ./i18n/i18n_de.properties (Optional) ........................................................................ 31 Error Behaviour....................................................................................................... 31 Step 10:
Improving the Application Structure.................................................................... 32
10.1. Preview .................................................................................................................. 32 10.2. ./index.html ............................................................................................................. 33 10.3. ./models/model.json (New file in new folder) ............................................................ 33 10.4. ./controllers/formatter.js........................................................................................... 34 10.5. ./controllers/App.controller.js ................................................................................... 35 10.6. ./views/App.view.xml ............................................................................................... 36 Error Behaviour....................................................................................................... 37 Step 11:
Property Formatting Using Data Types ............................................................... 38
11.1. Preview .................................................................................................................. 38 11.2. ./models/model.json ................................................................................................ 39 11.3. ./views/App.view.xml ............................................................................................... 39 11.4. ./controllers/formatter.js........................................................................................... 40 11.5. ./i18n/i18n.properties .............................................................................................. 40 11.6. ./i18n/i18n_de.properties (Optional) ........................................................................ 40 Explanation and Warning ........................................................................................ 41 Currency Symbols are Locale Specific .................................................................... 41 The Browser's Locale May Not Be The Application Language ................................. 41 Behaviour ............................................................................................................... 42 Step 12:
Validation Using the Message Manager............................................................... 43
12.1. Preview .................................................................................................................. 43 12.2. ./index.html ............................................................................................................. 44 Explanation ............................................................................................................. 44 Behaviour ............................................................................................................... 44 Step 13:
Element Binding ................................................................................................... 45
13.1. Preview .................................................................................................................. 45 13.2. ./views/App.view.xml ............................................................................................... 46 13.3. ./models/model.json ................................................................................................ 46 13.4. ./controllers/formatter.js........................................................................................... 47 13.5. ./i18n/i18n.properties .............................................................................................. 48 13.6. ./i18n/i18n_de.properties......................................................................................... 48
Explanation ............................................................................................................. 48 Step 14:
Expression Binding .............................................................................................. 50
14.1. Preview .................................................................................................................. 50 14.2. ./views/App.view.xml ............................................................................................... 51 14.3. ./models/model.json ................................................................................................ 51 Warning! ................................................................................................................. 52 Understanding Syntax Collisions ............................................................................. 52 Step 15:
Aggregation Binding Using A Template .............................................................. 53
15.1. Preview .................................................................................................................. 53 15.2. ./views/App.view.xml ............................................................................................... 54 15.3. ./controllers/App.controller.js ................................................................................... 55 15.4. ./controllers/formatter.js........................................................................................... 55 15.5. ./i18n/i18n.properties .............................................................................................. 56 15.6. ./i18n/i18n_de.properties (Optional) ........................................................................ 56 15.7. ./models/Products.json (New file) ............................................................................ 57 Step 16:
Aggregation Binding Using A Factory Function ................................................. 61
16.1. Preview .................................................................................................................. 61 16.2. ./views/App.view.xml ............................................................................................... 62 16.3. ./i18n/i18n.properties .............................................................................................. 63 16.4. ./i18n/i18n_de.properties (Optional) ........................................................................ 63 16.5. ./controllers/App.controller.js ................................................................................... 63
SAPUI5 Tutorial – Data Binding
Introduction In this tutorial, we will explain the concepts of data binding in SAPUI5. Data binding is a declarative programming technique in which the properties of a Model-ViewController (MVC) Model are associated with (or bound to) the properties of UI elements in an MVC View. The use of data binding follows as a consequence of the SAPUI5's use of the MVC design pattern, and within the context of this development framework, is the standard programming technique for presenting model-based information on the screen.
July 2015
1
SAPUI5 Tutorial – Data Binding
Prerequisites and Preparation This tutorial assumes you have access to the SAP Web IDE: either by having an SAP HANA trial , or by having a customer . If you have neither of these, then you can create a trial for free by visiting https://.hanatrial.ondemand.com/ You have already completed the SAPUI5 Walkthrough tutorial – currently available as a Beta version.
Initial Preparation for Tutorial Steps Under Local in your SAP Web IDE session, create a new folder called DataBinding. Right-click on the new DataBinding folder and select New Cloud Connectivity Configuration.
This will create a file called neo-app.json. You do not need to edit the contents of this file, but without it, your SAP Web IDE project will not be able to access the SAPUI5 libraries. SAP Web IDE may also automatically create several other files such as .project.json, ..project.json and .sap-ui-cachebuster-info.json. Do not edit or delete these files!
Testing the Developments Performed in each Tutorial Steps Once you have added the required coding for a tutorial step, select index.html file under the DataBinding folder and press the Run button in the toolbar. This will start the Application Preview in a new tab and you will be able to see your changes immediately. If you do not see the expected changes in the application preview tab, then this is either because you have not saved your changes, or some old information about the application is still in your browser's cache. If browser caching becomes an issue with App Preview, simply open a private browsing window to ensure you start from an empty cache. Alternatively, if you see an empty or partially rendered screen, you should open your browser's development tools to look in the JavaScript console for error message(s). It is helpful to leave the Application Preview tab open. Then when you have completed the next step of the tutorial, make sure that all your changes have been saved, and simply refresh the Application Preview to see the changes.
July 2015
2
SAPUI5 Tutorial – Data Binding
Increasing the Level of Logging Detail The normal approach to switching on SAPUI5 debug mode is to press Ctrl+Alt+Shift+P in the browser window or tab that is running the SAPUI5 application. This key combination brings up an information window in which you can check the "Use Debug Sources" checkbox. This causes the app to restart in debugging mode.
However, if you find that this key combination does not work then it is probably because the error in your coding has caused the SAPUI5 core to terminate and consequently, there is no SAPUI5 coding running anymore to respond to the Ctrl+Alt+Shift+P keyboard event. Therefore, under these circumstances and only under these circumstances, you can switch on debug mode programmatically.
July 2015
3
SAPUI5 Tutorial – Data Binding
Warning! This approach is only recommended in circumstances where the bug in your coding causes the SAPUI5 core to terminate! The reason is that since the following statements cause your app to run permanently in debug mode, it is easy to forget that you've got this statement in your coding. This can potentially result in apps being deployed to the production server with debug mode permanently switched on! If you end up deploying an app to production that is hard-coded to run in debug mode then: a) Don't come crying to SAP about it, b) Examine why such an app ed unnoticed through your QA testing procedure, and c) Consider why you think it necessary to have the SAPUI5 debug sources deployed on your production server… In circumstances where your coding does not cause the SAPUI5 core to terminate, you should switch on debug mode using the Ctrl+Alt+Shift+P approach because this activates debug mode only temporarily. Only if this fails should you use the following code: <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta charset="UTF-8">
SAPUI5 Data Binding Tutorial 5p2l1q <script id="sap-ui-bootstrap" src="../resources/sap-ui-core-dbg.js" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m" data-sap-ui-compatVersion="edge" data-sap-ui-preload="async" > <script> // Set log level to maximum and explicitly switch on the debug flag jQuery.sap.log.setLevel(jQuery.sap.log.LogLevel["DEBUG"]); jQuery.sap.debug(true); // -----------------------------------------------------------------------// Attach an anonymous function to the SAPUI5 'init' event sap.ui.getCore().attachInit(function () { ...SNIP...
First, by adding –dbg into the SAPUI5 core library name, the browser will directly load the debug version of the SAPUI5 core library. On its own however, this is not quite sufficient to get SAPUI5 to run in debug mode; we must also explicitly set the jQuery.sap.debug flag to true. This tutorial assumes that if you want to switch on debug mode, you will do so temporarily using the Ctrl+Alt+Shift+P approach.
July 2015
4
SAPUI5 Tutorial – Data Binding
Tutorial Steps Step 1:
No Data Binding
This exercise simply places some text on the screen using a standard sap.m.Text control. The text displayed by this control is a hard coded part of the control's definition; therefore, this is not an example of data binding!
1.1.
Preview
July 2015
5
SAPUI5 Tutorial – Data Binding
1.2. ./index.html (New file) Under the DataBinding folder, create an index.html file and within it place the following code: <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta charset="UTF-8">
SAPUI5 Data Binding Tutorial 5p2l1q <script id="sap-ui-bootstrap" src="../resources/sap-ui-core.js" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m" data-sap-ui-compatVersion="edge" data-sap-ui-preload="async" > <script> // -----------------------------------------------------------------------// Attach an anonymous function to the SAPUI5 'init' event sap.ui.getCore().attachInit(function () { // Create a text UI element that displays a hardcoded text string new sap.m.Text({ text : "Hi, my name is Harry Hawk" }). addStyleClass("sapUiSmallMargin"). placeAt("content"); });