Preview only show first 10 pages with watermark. For full document please download

6.1 Consuming A Gateway Service In Javascript

   EMBED


Share

Transcript

GW100 SAP NetWeaver Gateway Consuming a Gateway Service in JavaScript March, 2012 I N  N T E  E R  RN     A L Objectives  At the end of this chapter, you will understand: ! How Use the SAP UI5 JavaScript Libraries to Create a Gateway Service (Read-only) Agenda " Consuming an OData Service using the SAPUI5 JavaScript Libraries Setup Eclipse Workspace Create a new folder within your workspace. This will contain your SAP OData developments. Underneath this folder, create: •  An !"#$%&'()* file • Three more folders named +,#$*-, .!$/- and 0,"(1,**$1-. Define a Basic Web Page – 1/2 It is assumed that the SUPUI5 libraries are already installed on your web server under 2-345!6. Edit !"#$%&'()* and add the following coding to start with: 789:0;<=> '()*@ 7'()*@ 7'$3#@ Here’s the important part 7)$(3 '((4A$B5!CD!"#$%#&'()*+,-./! 1'2+/2+3!453/67/! 89 7(!(*[email protected]= :93(32EF=GH6 #$),72(!(*[email protected] Boot up SAPUI5, load a theme then load the required libraries 78AA I,3# EF=GH6J -$*$K( L,*# 1$M*$K(!," ('$)$ 3"# *,3# ('$ K,)),"-J (3N*$ 3"# 5%O *!N131!$- [email protected] 7-K1!4( !#D!:*)#;,#-''+:+<*)! +=)/3!+/>+8?*@*:1<,)+! -1KD!8:*);,A8F!9G8  :1<,)+9 &&& -"!4 &&& 7N,#P K*3--D!:*)$,H'6=!9 7#!C !#D!:I/..%+8?*@*:1<,)+! :<13!B8J'6/.:8D.,7I+42D'J'6/.B?:!9G8  :1<,)+9 7-K1!4( (P4$D!+/>+8?*@*:1<,)+! :<13!B8&'2+<'../<:8D.,7I+&'2+<'../+8?*@*:1<,)+! (P4$D!+/>+8?*@*:1<,)+! (P4$D!+/>+8?*@*:1<,)+! (P4$D!+/>+8?*@*:1<,)+! :<13!B8K,/L:8;,C;+,.,+,/:B?:!9G8  :1<,)+9 :<13!B8K,/L:8:I'LM.,7I+:B?:!9G8  :1<,)+9 :<13!B8K,/L:8:I'L%,<)'<+:B?:!9G8  :1<,)+9 :<13!B8K,/L:8:I'LH''N,27:B?:!9G8  :1<,)+9 The coding for models, views and controllers is separated into different files in order to make it easier to manage. Create a JavaScript Closure for the Model In the &2+,#$*- folder create M*!L'(H"M,+,#$*&R-. The following code creates a closure around the declaration of the OData model object hiding it as a private property and exposing it through method L$(+,#$*TU. The statement to create the OData model is highlighted. 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ 22 01$3($ 3 ),#$* ,NR$K( K*,-51$ 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ !"# ,S*!L'(+,#$* D T $%&'()*&T51!J-$1C!K$J5-$1J43--/,1#U V D 51! XX Y'((4Z22*,K3*',-(2-342[3($/3P2F\02Y] !"# #M*(W51! !"# #M*(W-$1C!K$ D -$1C!K$ XX YS*!L'(WH"M,1)3(!,"W222Y] D 5-$1 XX Y[^WGE>_Y] 22 F## #$M35*( 5-$1 !"# #M*(W5-$1 !"# #M*(W43--/,1# D 43--/,1# XX Y43--/,1#Y] 22 F## #$M35*( 5-$1 43--/,1# !"# ,+,#$* D &+, -34&5!&),#$*&,#3(3&:93(3+,#$*T#M*(W51! d #M*(W-$1C!K$J $"-.+J #M*(W5-$1J #M*(W43--/,1#U] #+(%#& V L$(+,#$* Z $%&'()*&TU V #+(%#& ,+,#$*] `J L$(+$(3#3(3 Z $%&'()*&TU V #+(%#& ,+,#$*&,+$(3#3(3(3E$1C!K$-&-K'$)3abc] ` ` `TUU] Create a Controller In the &20,"(1,**$1- folder create M*!L'(0,"(1,**$1&R-. In this example, the coding in the controller does nothing more than return an anonymous object having methods that return the name of each of the entity types. This functionality is used by a utility that creates a UI table for an entity type. 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ 22 9$M!"$ 3 K,"(1,**$1 M,1 '3"#*!"L S*!L'( H"M,1)3(!," 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ -34&5!&K,"(1,**$1TY-34&(13!"!"L&L/ebb&K,"(1,**$1&#,S*!L'(H"M,YJ T$%&'()*&TU V #+(%#& V Each controller is named using L$(S*!L'(>"(!(Pf3)$Z $%&'()*&01 2 #+(%#& 3S*!L'(34 56 an arbitrary namespace. L$(\,,g!"L>"(!(Pf3)$Z $%&'()*&01 2 #+(%#& 3\,,g!"L34 56 L$(F!14,1(>"(!(Pf3)$Z $%&'()*&01 2 #+(%#& 3F!14,1(34 56 `] `TUU U] Create Views for each Entity Type In the &2.!$/- folder create a file for each view called -',/F!14,1(-&R-, -',/S*!L'(-&R-, and -',/\,,g!"L-&R-. Here, we’ll use a utility function to create the required UI table using the methods found in the controller that return the entity type name. Each view needs to know the identity of its associated controller. 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ 22 F!14,1(- C!$/ 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ -34&5!&R-C!$/TY-34&(13!"!"L&L/ebb&C!$/&-',/F!14,1(-YJ V L$(0,"(1,**$1f3)$Z $%&'()*&TU V #+(%#& Y-34&(13!"!"L&L/ebb&K,"(1,**$1&#,S*!L'(H"M,Y] `J K1$3($0,"($"(Z $%&'()*&T,0,"(1,**$1U V #+(%#& N5!*#;3N*$S1,)+$(3#3(3T,S*!L'(+,#$*J ,0,"(1,**$1&L$( F!14,1(>"(!(Pf3)$TUU] ` ` U] This value is then passed to method K1$3($0,"($"(TU This function is repeated for each view with the entity type name changed as required. Utility to Create a UI Table from OData Metadata – 1/2 In the &2.!$/- folder create the file 5!W5(!*!(!$-&R-. 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ 22 01$3($ 3 GH (3N*$ N3-$# ," -544*!$# >"(!(P ;P4$ "3)$ 3"# :93(3 )$(3#3(3 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ !"# N5!*#;3N*$S1,)+$(3#3(3 D $%&'()*&T,+,#$*J $"(!(Pf3)$U V !"# ,+$(393(3 D ,+,#$*&L$(+$(3#3(3TU D ,+$(393(3&"3)$-43K$ d Y&Y d $"(!(Pf3)$] !"# ('!->"(!(P;P4$ !"# ('!->"(!(PE$(f3)$ D YY] D ac] !"# K,*I!-( !"# ! D bJ R D b] 22 I,,4 31,5"# $"(!(P (P4$$*# T!Db] !7,+$(393(3&$"(!(P;P4$&*$"L('] !ddU V 22 h3C$ /$ M,5"# ('$ $"(!(P (P4$ /$i1$ *,,g!"L M,1j )$ T,+$(393(3&$"(!(P;P4$a!c&"3)$ kk ,+$(393(3&$"(!(P;P4$a!c&"3)$ DDD $"(!(Pf3)$U V 22 G-$ ('$ M5**P B53*!M!$# $"(!(P "3)$ (, #$($1)!"$ ('$ $"(!(P -$( "3)$ $*# TRDb] R7,+$(393(3&$"(!(P0,"(3!"$1abc&$"(!(PE$(&*$"L('] RddU V )$ T,+$(393(3&$"(!(P0,"(3!"$1abc&$"(!(PE$(aRc&$"(!(P;P4$ DDD ('!->"(!(P;P4$U V ('!->"(!(PE$(f3)$ D ,+$(393(3&$"(!(P0,"(3!"$1abc&$"(!(PE$(aRc&"3)$] ` ` Utility to Create a UI Table from OData Metadata – 2/2 Continuation of the coding in &2.!$/-25!W5(!*!(!$-&R-. 22 I,,4 31,5"# ('$ $"(!(P (P4$ 41,4$1(!$- K1$3(!"L 3 (3N*$ K,*5)" M,1 $3K' $*# TRDb] R7,+$(393(3&$"(!(P;P4$a!c&41,4$1(P&*$"L('] RddU V 22 S,1 -!)4*!K!(PJ )!-- ,5( K,)4*$% (P4$)$ T,+$(393(3&$"(!(P;P4$a!c&41,4$1(PaRc&(P4$&-5N-(1!"LTbJlU DD Y>#)&YU V K,*I!-(&45-'TV*3N$* Z ,+$(393(3&$"(!(P;P4$a!c&41,4$1(PaRc&"3)$J ($)4*3($Z ,+$(393(3&$"(!(P;P4$a!c&41,4$1(PaRc&"3)$`U] ` ` ` ` 22 01$3($ 3 (3N*$ 5-!"L ('$ K,*5)" *!-( K1$3($# 3N,C$ !"# ,;3N*$ D &+, -34&5!&(3N*$&93(3;3N*$TV K,*5)"-Z K,*I!-( `U] 22 N!"# ),#$* (, ;3N*$ ,;3N*$&-$(+,#$*T,+,#$*&L$(+,#$*TUU] ,;3N*$&N!"#FLL1$L3(!,"TY1,/-YJ ('!->"(!(PE$(f3)$U] 1$(51" ,;3N*$] `] Complete the JavaScript Coding in the Web Page – 1/4 The remainder of the !"#$%&'()* web page now needs to be completed.  After the last 7-K1!4(@ element, add the following: 7-K1!4(@ 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ 22 H"-(3"(!3($ C!$/- #$K*31$# !" ('$ 3N,C$ &2C!$/-2Q&R- M!*$ 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ !"# M*!L'(-.!$/ D -34&5!&R-C!$/TY-34&(13!"!"L&L/ebb&C!$/&-',/S*!L'(-YU] !"# 3!14,1(-.!$/ D -34&5!&R-C!$/TY-34&(13!"!"L&L/ebb&C!$/&-',/F!14,1(-YU] !"# N,,g!"L-.!$/ D -34&5!&R-C!$/TY-34&(13!"!"L&L/ebb&C!$/&-',/\,,g!"L-YU] Create an instance of each view. Notice that views need to be identified by their fully qualified name. Complete the JavaScript Coding in the Web Page – 2/4 The UX3 Shell object defines the entire screen layout. The /,1g-$(H($)- array holds the menu structure. 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ 22 01$3($ ('$ 5%O -'$** T"3C!L3(!," 3"# K,"($"( K,"(3!"$1 U 22 QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ !"# ,E'$** D &+, -34&5!&5%O&E'$**TY)PE'$**YJ V 344HK,"ZY'((4Z22///&-34&K,)2L*,N3*2!)3L$-2EF=I,L,&L!MYJ 344;!(*$ZYEF=GH6 H"($1M3K$ (, [3($/3PYJ /,1g-$(H($)-Za &+, -34&5!&5%O&f3C!L3(!,"H($)T V g$PZY/!WS*!L'(H"M,YJ ($%(ZYS*!L'( H"M,1)3(!,"YJ -5NH($)-Za &+, -34&5!&5%O&f3C!L3(!,"H($)TVg$PZY/!WS*!L'(H"M,WS*!L'(-YJ ($%(ZYS*!L'(-Y`UJ &+, -34&5!&5%O&f3C!L3(!,"H($)TVg$PZY/!WS*!L'(H"M,WF!14,1(-YJ($%(ZYF!14,1(-Y`UJ &+, -34&5!&5%O&f3C!L3(!,"H($)TVg$PZY/!WS*!L'(H"M,W\,,g!"L-YJ($%(ZY\,,g!"L-Y`U c `U cJ 43"$\31H($)-Z a &+, -34&5!&K,1$&H($)TVg$PZY4NW4$,4*$YJ ($%(ZY=$,4*$Y`U cJ *,L,5(Z $%&'()*&TU V 3*$1(TY\P$ NP$&&&YU] ` ` U] Complete the JavaScript Coding in the Web Page – 3/4 The L$(0,"($"(TU function examines the key of the selected menu item and returns the appropriate view instance. 22 =3L$ K,"($"( K1$3(!," A M,1 $3K' /,1g-$( !($) ('$ K,"($"( !- #$M!"$# '$1$ !"# )0,"($"( D V`] !"# L$(0,"($"( D $%&'()*&Tg$PU V 22 HM K,"($"( !- 3*1$3#P K1$3($#J 1$(51" !( #!1$K(*P )$ T)0,"($"(ag$PcU #+(%#& )0,"($"(ag$Pc] Tg$P DDD Y/!WS*!L'(H"M,W\,,g!"L-YU V )0,"($"(ag$Pc D N,,g!"L-.!$/] ` )$ +-.+ )$ Tg$P DDD Y/!WS*!L'(H"M,WF!14,1(-YU V )0,"($"(ag$Pc D 3!14,1(-.!$/] ` +-.+ )$ Tg$P DDD Y/!WS*!L'(H"M,WS*!L'(-YU V )0,"($"(ag$Pc D M*!L'(-.!$/] ` #+(%#& )0,"($"(ag$Pc] ` The returned view instance is then displayed in the UX3 shell’s content area. Complete the JavaScript Coding in the Web Page – 4/4  Attach a function to the UX3 Shell’s ^,1g-$(H($)E$*$K($# event that returns the selected view instance, then set the initial view. 22 ^'$" ('$ 5-$1 -$*$K(- 3 /,1g-$( !($)J 45( ('$ 1$-4$K(!C$ K,"($"( !"(, ('$ -'$**i22 )3!" 31$3 ,E'$**&3((3K'^,1g-$(H($)E$*$K($#T$%&'()*&T,>C$"(U V !"# g$P D ,>C$"(&L$(=313)$($1TYg$PYU] ,E'$**&-$(0,"($"(TL$(0,"($"(Tg$PUU] `U] 22 E$( ('$ !"!(!3* K,"($"( ,M ('$ E'$** A ('$ h,)$ :C$1C!$/ !- -$*$K($# !"!(!3**P ,E'$**&-$(0,"($"(TL$(0,"($"(TY/!WS*!L'(H"M,WS*!L'(-YUU] 22 =*3K$ ('$ E'$** !"(, ('$ 7#[email protected] $*$)$"( #$M!"$# N$*,/ ,E'$**&4*3K$F(TY-'$**F1$3YU] 72-K1!4(@ 72'$3#@ Finally, place the output of the UX3 Shell into the HTML 7#[email protected] called -'$**F1$3. Finished Application Hands-on Exercise Exercise 14 Create a Gateway Application Using SAPUI5 JavaScript Libraries Summary You should now understand: ! How Use the SAP UI5 JavaScript Libraries to Create a Gateway Service (Read-only)  © 2012 SAP AG. All rights reserved No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. 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, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.  Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, StreamWork, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company. Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase, Inc. Sybase is an SAP company.  All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. The information in this document is proprietary to SAP. No part of this document may be reproduced, copied, or transmitted in any form or for any purpose without the express prior written permission of SAP AG. This document is a preliminary version and not subject to your license agreement or any other agreement with SAP. This document contains only intended strategies, developments, and functionalities of the SAP® product and i s 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. SAP assumes no responsibility for errors or omissions in this document. SAP does not warrant the accuracy or completeness of the information, text, graphics, links, or other items contained within this material. This document is provided without a warranty of any kind, either express or 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.