This Guide explains how to build a simple SAPUI5 application, as it was shown in the CodeJam at Antwerp on Feb 20th 2014.

Creating the SAPUI5 Application

  1. Create a new SAPUI5 project by right clicking in the “Project Explorer” and selecting “New” –> “Project…”
  2. In the New Project Dialog select “Application Project” in the “SAPUI5 Application Development” folder.
  3. Name the project, select “Mobile” as “Target Device” and check “Create an Initial View“. Then click on “Next >”
  4. Create the initial view. In our example we name it “App“. We choose “XML” as “Development Paradigm”. Then click on “Finish”
  5. Let’s have a short look at our applications file structure. There are three important files. The “index.html” which is the HTML page which displays the application. An “App.view.xml” which is an XML file which describes the view and the “App.controller.js” which implements the views logic.
  6. To test your application you can always right click on the project and select: “Run As” –> “Web App Preview”

App View

The App view should contain a split app, which contains a list on the left hand side (to display the products) and a navigation container on the right hand sind (to display details). Therefore we edit the index.html and App.view.xml as shown below.

index.html

                          
 

App.view.xml

                                                                                                                                                          

Routing

To enable routing within the application you need to create an application router. We place this in the javascript section of the index.html file (the same

 jQuery.sap.require("sap.ui.core.routing.Router"); jQuery.sap.require("sap.m.routing.RouteMatchedHandler"); var oRouter = new sap.ui.core.routing.Router({      product: {           pattern: "product/{id}",           view: "Product",   },   supplier: {   pattern: "supplier/{id}",   view: "Supplier"   }   }, {   targetControl: "navContainer",   targetAggregation: "pages",   targetParent: "productsApp",   viewType: "XML",   viewPath: "codejamapplication",   clearTarget: false   });   var oRouteHandler = new sap.m.routing.RouteMatchedHandler(oRouter);   oRouter.register("router"); oRouter.initialize(); 

The oRouter.initialize() should be done after the application view has been placed on the screen (app.placeAt(“content”)) All the other code needs to be executed before the app view is initialized.

Model

To connect your application to the backend, you have to define a Model. We do this in the index.html file:

 var oModel = new sap.ui.model.odata.ODataModel("https://54.224.71.58:50000/sap/opu/odata/sap/ZGWSAMPLE_SRV");   sap.ui.getCore().setModel(oModel); 

This definition should be placed before the lines where the application view is instantiated.

Service URLs need to have the same host as the application. Else the requests are blocked by the browser for security reasons. To overcome this you can use the SAPUI5 proxy servlet, which is enabled by default for all SAPUI5 Application projects. You can write the url like

 new sap.ui.model.odata.ODataModel("proxy/http/54.224.71.58:50000/sap/opu/odata/sap/ZGWSAMPLE_SRV"); 

Products and Supplier View

To display the details we defined two routes for which we need to define the views.

To use all the bindings which are define din this view, you have to enable the extended binding syntax. This can be done by adding “data-sap-ui-xx-bindingSyntax=”complex” “ to the bootstrap script tag.

Supplier.view.xml

                                 

Product.view.xml

                                    

In this coding you also see the data binding syntax. You can write the entities properties in curly braces. You can also define complex binding structures like mixing strings and bindings. To enable this, you have to add an additional attribute to the bootstrap in the index.html file.

We also still miss the databinding in the App.view.xml which is shown below.

                                                   

We have now created all the views, that we need for our application. We still miss the logic.

Controllers

To logic of the application in imiplemented in the controllers. Here we handle the navigation and data binding.

App.controller.js

 sap.ui.controller("codejamapplication.App", {   onInit: function() {   this.oRouter = sap.ui.core.routing.Router.getRouter("router");   },   selectProduct: function(oEvent) {   var oBindingContext = oEvent.getSource().getBindingContext();   this.oRouter.navTo("product", { id: oBindingContext.getProperty("ProductId") });   } }); 

Product.controller.js

 sap.ui.controller("codejamapplication.Product", {   onInit: function() {   this.oRouter = sap.ui.core.routing.Router.getRouter("router");   this.oRouter.attachRouteMatched(function(oEvent) {   var sRoute = oEvent.getParameter("name"),   oView = oEvent.getParameter("view");   if (sRoute === "product") {   oView.setBusyIndicatorDelay(0);   oView.setBusy(true);   sap.ui.getCore().getModel().createBindingContext("/ProductCollection('" + oEvent.getParameter('arguments').id + "')", function(oContext) {   oView.setBindingContext(oContext);   oView.setBusy(false);   });   }   });   },   showSupplier: function(oEvent) {   var oBindingContext = oEvent.getSource().getBindingContext();   this.oRouter.navTo("supplier", { id: oBindingContext.getProperty("SupplierId") });   } }); 

Supplier.controller.js

 sap.ui.controller("codejamapplication.Supplier", {   onInit: function() {   this.oRouter = sap.ui.core.routing.Router.getRouter("router");   this.oRouter.attachRouteMatched(function(oEvent) {   var sRoute = oEvent.getParameter("name"),   oView = oEvent.getParameter("view");   if (sRoute === "supplier") {   oView.setBusyIndicatorDelay(0);   oView.setBusy(true);   sap.ui.getCore().getModel().createBindingContext("/BusinessPartnerCollection('" + oEvent.getParameter('arguments').id + "')", function(oContext) {   oView.setBindingContext(oContext);   oView.setBusy(false);   });   }   });   },   handleNavBack: function() {   var oHistory = sap.ui.core.routing.History.getInstance();   if (oHistory.getPreviousHash()) {   window.history.go(-1);   }   } }); 

New NetWeaver Information at SAP.com

Very Helpfull

User Rating: Be the first one !