SAP UI5和Angular里控制器(Controller)实现逻辑比较

简介: SAP UI5和Angular里控制器(Controller)实现逻辑比较
+关注继续查看

Let’s first refresh our memory on SAPUI5 controller. I have a simple xml view which only contains a button:

And a simple controller

sap.ui.define(["sap/ui/core/mvc/Controller"], function(Controller){

"use strict";

return Controller.extend("buttontutorial.view.simple",{

onInit : function() {

 debugger;

}

 });}

);

due to the attribute controllerName=”buttontutorial.view.simple” in XML view, the controller instance is created and connect with XML view instance by UI5 framework:


image.png

And we can use JavaScript code in console to list the larget number of attributes belonging to created controller instance:

 

for( var name in this ) { console.log("attribute: " + name + " value: " + this[name]);}

image.png

Or you can simply type “this.” in console, and see there are lots of methods available for controller instance:

image.png

For example, byId method of controller instance is widely used, if you type this.byId in console, you can see its implementation just delegates the call to this.oView.byId.



image.png

This makes sense since every controller instance holds a reference to its host view via oView, and the connection between controller and its view is established in function connectToView:


image.png

Angular Controller

You can play with the sample Angular application from this url.


image.png

It consists of 31 lines of source code:


 

   

   Angular.js Example

   

   </div><div data-lake-id="aa70354e357fbf1b6884705c5cb04c53">      function NameCtrl($scope){</div><div data-lake-id="217161ed7693c89bbc1b9acfceb84cf9">        $scope.names = ['ABAP', 'Java'];</div><div data-lake-id="7a4fc359641e169fa78fd7043b660d7b">        $scope.addName = function() {</div><div data-lake-id="4c3d3368b8b167223db130b872321609">          if( $scope.names.indexOf($scope.enteredName) != -1){</div><div data-lake-id="1bc79f45884307fb15dc0e58a44af21b">            alert("duplicate key is not allowed in list: " + $scope.enteredName);</div><div data-lake-id="90fbffb45c585208d52a2b7b402e2ba8">            $scope.enteredName = '';</div><div data-lake-id="8c5808e5cb9d322da50e7c8fdae9cc38">            return;</div><div data-lake-id="dc8b72782977f587bd9241ac9f552f93">          }</div><div data-lake-id="0a826aaa26a3368865ce4cbd88a49c04">          $scope.names.push($scope.enteredName);</div><div data-lake-id="7ae213f8cf1be601d0a750e32e596606">          $scope.enteredName = '';</div><div data-lake-id="35137fb4eddd44e1b9ca369ff3b37420">        };</div><div data-lake-id="0e7a1c62e0f49db92f12855b88eccb3e">    }</div><div data-lake-id="52ad988d3bac67ae08f1eb8bce5b31a9">    

 

 

   

         

  • {{name}}
  •      

       

       

         

         

       

     

    When you type a new language in input field and click “Add” button, the language will be added into list above:

    image.png

    Let me first briefly introduce the idea of source code, then I will go through with each point in detail.

    image.png

    (1) controller instance initialization


    During Angular bootstrap phase, due to this line of source code in html, , Angular will create a new controller instance in line 5327. You can consider $controller as a factory function.


    image.png

    Let’s have a look at the content of argument locals for factory function:

    image.png

    The most important attribute is $scope, which is passed into function NameCtrl defined by us:

    image.png

    Once our application code is executed, controller instance is created. However, after checking it in Chrome, I found it is just a dummy instance without any important attribute. Instead, the data model and addName function are appended and available in current scope


    image.png

    (2) How addFunction available in scope object will be called when add button is called


    Based on study result in step1, the addName function is located in scope object. My question is, when I press add button, why is it called?


    In previous blog Compare Event handling mechanism: SAPUI5 and Angular, I already introduced that Angular does event handler registration automatically for us, as long as it detects the directive like this:


    .

    Actually I have made modifications on angular.js, adding more trace so that you can easily find where Angular achieves event registration under the hood:


    image.png

    image.png

    So it is called as expected:


    image.png

    Summary

    UI5 controller instance has a large number of useful functions available to use, and for Angular, controller instance is just a dummy one: data model and event handler function are located in scope object instead.


    相关文章
    |
    2月前
    |
    缓存 Java C++
    SAP 电商云 Spartacus UI Angular UI 和 Accelerator JSP UI 的混合使用
    Spartacus 在技术和架构( library 发布方式 vs 模板发布方式,headless vs embedded,Angular vs JSP 技术栈)等各方面,都是一种全面的从 Accelerator 的完整范式转变。因此,对于 Commerce Cloud 已经使用 Accelerator 的客户来说,并没有直接的方法可以从基于 Accelerator 的 Storefront 迁移到使用 Spartacus library 的店面。
    30 0
    |
    2月前
    |
    中间件
    Angular 里 HTTP 请求和响应结构的拦截器(interceptors)在 SAP Spartacus 中的应用
    Angular 里 HTTP 请求和响应结构的拦截器(interceptors)在 SAP Spartacus 中的应用
    38 0
    |
    2月前
    |
    前端开发 API
    Angular HTTPClient API 在 SAP 电商云中的使用
    Angular HTTPClient API 在 SAP 电商云中的使用
    23 0
    |
    6月前
    |
    JavaScript
    angular从iframe里面调用父页面的controller的方法
    angular从iframe里面调用父页面的controller的方法
    |
    容器
    SAP 电商云 Spartacus UI Angular Component 动态创建的单步调试
    SAP 电商云 Spartacus UI Angular Component 动态创建的单步调试
    SAP 电商云 Spartacus UI Angular Component 动态创建的单步调试
    |
    API 网络架构
    如何找到 SAP 电商云 Spartacus UI 产品明细界面对应的 Angular 实现 Component 名称
    如何找到 SAP 电商云 Spartacus UI 产品明细界面对应的 Angular 实现 Component 名称
    如何找到 SAP 电商云 Spartacus UI 产品明细界面对应的 Angular 实现 Component 名称
    |
    JavaScript 安全
    Angular 事件绑定语法在 SAP Spartacus Popover Component 中的一个应用
    Angular 事件绑定语法在 SAP Spartacus Popover Component 中的一个应用
    Angular 事件绑定语法在 SAP Spartacus Popover Component 中的一个应用
    SAP Spartacus B2B user列表对应的Angular Component
    SAP Spartacus B2B user列表对应的Angular Component
    127 0
    SAP Spartacus B2B user列表对应的Angular Component
    |
    JavaScript 前端开发
    SAP UI5和Angular的函数防抖(Debounce)和函数节流(Throttle)实现原理介绍
    这是Jerry 2021年的第 11 篇文章,也是汪子熙公众号总共第 282 篇原创文章。
    SAP UI5和Angular的函数防抖(Debounce)和函数节流(Throttle)实现原理介绍
    SAP Spartacus中使用到的Angular ModuleWithProviders类型
    SAP Spartacus中使用到的Angular ModuleWithProviders类型
    72 0
    SAP Spartacus中使用到的Angular ModuleWithProviders类型
    推荐文章
    更多