前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SAP UI5 - 也支持双向绑定

SAP UI5 - 也支持双向绑定

作者头像
Jerry Wang
发布2019-05-31 10:59:02
5490
发布2019-05-31 10:59:02
举报

版权声明:本文为博主汪子熙原创文章,未经博主允许不得转载。

有的程序员和我讨论其现在的前端框架时,说Angular比UI5高级,因为前者支持双向绑定。

然而UI5也是支持双向绑定的,看下面这张图里Data Binding->Two way data binding一览,SAP UI5和Angular一样都是支持的哦:

clipboard1
clipboard1

并且有代码为证:

clipboard2
clipboard2

实际上,我专门在SAP社区上写过一篇文章,详细比较了Angular和UI5数据双向绑定的差异:

I first list a comparison from my point of view and will illustrate it in detail.

clipboard3
clipboard3

I use the following simple Angular application which only contains 11 lines to demonstrate the data binding implementation of Angular.

clipboard4
clipboard4

Here I have a input element marked with attribute ‘ng-model=”name”‘, which means I tell Angular that I have declared a data model with name “name”. Then for “Hello {{name}}”, it is actually a template and I tell Angular to render the page with static text “Hello” plus the actual content of input field. You can test the application here. Every time you type something in the input field, the characters you have typed will be displayed after “Hello”:

clipboard5
clipboard5

In this blog, I will explain how these 11 lines of codes have implemented the data binding feature, with the help of Angular framework code. When you launch the application for the first time, the application will look like below. Although you only saw blank field in input field and initial value after “Hello”, some logic has already been executed under the hood. I will first introduce how the initial value is rendered in html page. How initial value is rendered in html page

  1. Like UI5, Angular has also its bootstrap phase. During this phase, Angular will traverse the HTML DOM tree. Once it detects the input element marked with attribute “ng-model”, it will create a watch object for current scope:
clipboard6
clipboard6

The watcher object is just an object consisting of several functions. So far the last attribute points to a function, and the function get is used to extract value from data model.

clipboard7
clipboard7
clipboard8
clipboard8
  1. And you have already noticed that although we do not explicitly specify event handler for input field, however it can still react to our input. Why? Angular framework has registered a listener on input event for us. We will go through the implementation of listener later. So now for the input element, we have one watcher and one listener.
clipboard9
clipboard9
  1. Again a second watcher is created for text node, “Hello {{name}}”. So now we have two watchers created ans stored in watchers array of current scope object.
clipboard10
clipboard10
  1. Still in bootstrap phase, the function $apply of scope is called.
clipboard11
clipboard11

Within this function, there is a DO loop to handle all watcher object of current scope one by one:

clipboard12
clipboard12

Since I haven’t typed anything in input field, I have only the static text “Hello” to be displayed:

clipboard13
clipboard13
clipboard14
clipboard14

Before line 5624 is executed:

clipboard15
clipboard15

After executed:

clipboard16
clipboard16

So far, the initial page display logic is introduced. How the value from Input field can flow to Hello text field Now I have typed “A” in input field:

clipboard17
clipboard17

Since I have explained previously that Angular has registered a listener for input field, so now it is called:

clipboard18
clipboard18

In this event handler, scope.$apply will be called.

clipboard19
clipboard19

You still remember the fact that all watchers will be handled within scope.$apply? This time, the input character “A” is extracted from input field as model:

clipboard20
clipboard20
clipboard21
clipboard21

Since now the template “Hello {{name}}” has already been filled with actual value, it is ready to render it:

clipboard22
clipboard22

After line 5624 is executed, you will see “Hello A” in UI:

clipboard23
clipboard23
clipboard24
clipboard24
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档