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

简介:

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

<core:View xmlns:core="sap.ui.core" xmlns:common="sap.ui.commons" controllerName="buttontutorial.view.simple">
<common:Button text="Jerry" id="jerryButton"/>
</core:View>

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:

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]);}

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

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.

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:

Angular Controller

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

It consists of 31 lines of source code:

<html ng-app>
  <head>
    <meta charset="utf-8">
    <title>Angular.js Example</title>
    <script src="angular/angular.js"></script>
    <script>
      function NameCtrl($scope){
        $scope.names = ['ABAP', 'Java'];
        $scope.addName = function() {
          if( $scope.names.indexOf($scope.enteredName) != -1){
            alert("duplicate key is not allowed in list: " + $scope.enteredName);
            $scope.enteredName = '';
            return;
          }
          $scope.names.push($scope.enteredName);
          $scope.enteredName = '';
        };
    }
    </script>
  </head>
  <body ng-controller="NameCtrl">
    <ul>
      <li ng-repeat="name in names">{{name}}
      </li>
    </ul>
    <form ng-submit="addName()">
      <input type="text" ng-model="enteredName">
      <input type="submit" value="add">
    </form>
  </body>
</html>

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

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

(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.

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

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

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:

(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:



So it is called as expected:

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.

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
1月前
|
存储 数据管理 数据处理
SAP CRM 里 Attachment 搜索的实现逻辑介绍
SAP CRM 里 Attachment 搜索的实现逻辑介绍
19 0
|
6月前
|
Web App开发 开发框架 前端开发
SAP Fiori Elements 应用中 table id 的生成逻辑
SAP Fiori Elements 应用中 table id 的生成逻辑
51 0
|
1月前
|
BI 数据库
SAP ABAP ALV 报表单击某列后执行某段 ABAP 逻辑的实现方式 - hotspot 行为实现试读版
SAP ABAP ALV 报表单击某列后执行某段 ABAP 逻辑的实现方式 - hotspot 行为实现试读版
24 0
|
6月前
|
缓存 索引
SAP ABAP 系统传输请求(Transport Request)导入到其他系统背后执行的逻辑
SAP ABAP 系统传输请求(Transport Request)导入到其他系统背后执行的逻辑
89 0
|
6月前
|
XML JSON JavaScript
SAP Fiori Elements 应用里标准模板 XML Fragment 加载的逻辑和 XMLPreprocessor 的作用
SAP Fiori Elements 应用里标准模板 XML Fragment 加载的逻辑和 XMLPreprocessor 的作用
52 0
|
6月前
|
XML JavaScript 前端开发
SAP UI5 Fiori Elements annotation 的解析逻辑 AnnotationParser.js
SAP UI5 Fiori Elements annotation 的解析逻辑 AnnotationParser.js
40 0
|
6月前
|
XML JavaScript 前端开发
SAP UI5 Fiori Elements annotation 文件序列化成 DOM 对象的逻辑
SAP UI5 Fiori Elements annotation 文件序列化成 DOM 对象的逻辑
29 0
|
6月前
|
Web App开发 资源调度 开发者
SAP Fiori Elements 应用 OData 元数据请求 url 里的模型名称决定逻辑
SAP Fiori Elements 应用 OData 元数据请求 url 里的模型名称决定逻辑
49 0
|
6月前
|
IDE 开发工具
SAP UI5 extension project 的概念以及如何在 WebIDE 里创建 view 和 controller extension
SAP UI5 extension project 的概念以及如何在 WebIDE 里创建 view 和 controller extension
53 0
|
6月前
|
存储
SAP CRM Text customizing 的读取逻辑
SAP CRM Text customizing 的读取逻辑
22 0