1. 程式人生 > >SAPUI5拓展標準應用的Controller原理分析及應用實踐

SAPUI5拓展標準應用的Controller原理分析及應用實踐

這裡寫圖片描述

前言

鑑於SAP S/4 HANA的世界性普及,外加中國區雲服務的正式落地,作為SAP 移動端使用者體驗的自有H5框架,也愈加備受關注, 目前SAP大部分產品都是基於SAP Fiori進行定製開發,因此SAP Fiori也提供給客戶大量的標準應用,只需要基礎運維人員進行安裝,即可實現實施應用。

於此同時,基於業務場景的個性化的開發不可或缺,如果能在原理邏輯上稍加修改,這樣可以減少很多時間和成本,所以今天我們就來看一下SAP UI5如何實現controller的拓展。

實驗分析

SAPUI5是通過合併的方法拓展一個基礎的Controller例項, 也就是說會合並標準應用的controller和我們自定義的controller,並應用到我們的拓展應用當中。

由此可見,SAPUI5的拓展方式並不是應用繼承,如果在自定義(拓展的)controller中定義了和標準controller同樣的名字,標準controler的方法會被覆蓋掉。

但是對於生命週期類的方法是例外的,比如:onInit, onExit, onBeforeRendering, onAfterRendering。這些方法並不會覆蓋,會被執行兩次。

onInit and onAfterRendering: 這兩個方法順序為: 標準 —》 自定義

onExit and onBeforeRendering: 這兩個方法為: 自定義 ——》 標準。

接下來我們我們看一具體的例子。

這是一個標準的controller.

sap.ui.controller("samples.components.ext.sap.Main", {
    onInit : function () {
        console.log("samples.components.ext.sap.Main - onInit");
    },

    doSomething: function() {
        alert("this is an original standard action");
    },

    doSomeStandardAction: function() {
        alert("this is another original standard action"
); } });

這是一個自定義拓展的controller.

sap.ui.controller("samples.components.ext.customer.CustomMain", {
    onInit : function () {
        console.log("samples.components.ext.customer.CustomMain - onInit");
    },

    doSomething: function() {
        alert("this is a customer action");
    },

    doSomeCustomAction: function() {
        alert("this is another customer action");
    }
});

在component.js中指定合併的兩個controller檔案。這個配置會在自定義拓展應用的component檔案中,程式碼如下:

customizing: {  
    "sap.ui.controllerExtensions": {
        "samples.components.ext.sap.Main": {
            controllerName: "samples.components.ext.customer.CustomMain"
        }
    },
    .....some more content

在拓展應用中,我們可以看到會執行onInit方法兩次:

samples.components.ext.sap.Main - onInit
samples.components.ext.customer.CustomMain - onInit

對於doSomething方法,我們在自定義拓展的controler重新定義了一下,這個標準的controler就會被覆蓋,我們會看到執行了:

 alert("this is a customer action");

但是對於doSomeCustomAction, doSomeStandardAction兩個方法並沒有重名,所以不會被覆蓋,兩者都會執行。

對於自定義的應用,使用Sap 官方webIDE開發效率上非常高,但是如果用Eclipse開發,會存在一些問題,需要做出調整才能在eclipse上執行起來並拓展,所以這裡建議大家使用webIDE進行開發。

個人微信公眾號

這裡寫圖片描述