SAP UI5和Vue的数据双向绑定实现原理比较

简介: SAP UI5和Vue的数据双向绑定实现原理比较

Two-way data binding in UI5


Two way data binding test: Control property change leads to model field change


Two way data binding test: model field change leads to control property change


Two way data binding in Vue


Two way data binding test: model field change leads to control property change


Two way data binding test: Control property change leads to model field change


Step1 – Model directive detection


Step2 – Generate source code of event handler


Step3 – Register event handler into physical DOM element


Step4 – Model field will be changed in onInput event handler


Recently when I do self study on Vue I find many articles in the internet with full of praise on Vue‘s reactive Two-Way Data binding trait. This fact makes me recall my self-study on UI5 early in year 2013 and at that time, the Two-Way Data binding was already supported by UI5.


Two-way data binding in UI5

I have once already compared the Data Binding mechanism in my blog: Compare Data Binding mechanism: SAPUI5 and Angular.


Now I will reuse the button created for my blog Use Proxy pattern to make better image loading behavior in UI5 to recall UI5 two way data binding. I have a simply button defined in XML view:

image.pngimage.pngimage.pngimage.pngThe reason is: when the control property is bound to model field via this line in onInit function in my controller:


utton.bindProperty(“text”, “/field_for_text”);


A model change handler is registered as listener for model change event, which will be raised when refresh function is called. Inside this change handler, the control property will be updated based on the change of model field.

image.pngTwo way data binding in Vue

The version of Vue I am using is 2.3.4.

I am just using the hello world example provided in Vue tutorial to inspect its two way data binding trait.

image.pngTwo way data binding test: model field change leads to control property change

Change model field in console via code:

jerry.$data.message = “Jerry change in Console”;

image.pngimage.pngInside this function the native implementation of Object.defineProperty is utilized to registered a custom setter to the reactive field, which will be called whenever the change happens on that field.

image.pngWhen you change the value of this field in console,

the old value “Hello World” is overwritten by the new value “Jerry change in Console”, and dep.notify will trigger the very control involved in this model change:

image.pngimage.png

Two way data binding test: Control property change leads to model field change

I made some small changes on the example:image.pngNow whenever I type something in input field, the model field message is changed as well. How is this achieved in Vue?


Step1 – Model directive detection

In mount phase the directive v-model is parsed, extracted and stored in the attribute directives of input element:

image.png

Step2 – Generate source code of event handler for input field based on extracted directive in previous stepimage.pngStep3 – Register event handler into physical DOM element

The registration is done using native function addEventListener.image.pngimage.pngSummary

Fundamentally speaking, per above analysis, both UI5 and Vue uses the Observer pattern to implement the two way data binding. Only the slight difference is that for the notification between control and model, UI5 uses its own Observer code for bi-direction. In Vue, the notification from model to control is implemented on its own as well, whereas the opposite from control to model in the example of this blog is implemented via HTML native event.


相关文章
|
5天前
|
前端开发
Element UI 【实战】纯前端对表格数据进行增删改查(内含弹窗表单、数据校验、时间日期格式)
Element UI 【实战】纯前端对表格数据进行增删改查(内含弹窗表单、数据校验、时间日期格式)
27 6
|
17天前
|
前端开发 JavaScript
Vue底层实现原理总结
Vue底层实现原理总结
|
3天前
|
JavaScript
vue + element UI 表单中内嵌自定义组件的表单校验触发方案
vue + element UI 表单中内嵌自定义组件的表单校验触发方案
18 5
|
3天前
Element UI【级联选择器】el-cascader 获取选中内容的 label 数据,鼠标悬浮显示超长内容
Element UI【级联选择器】el-cascader 获取选中内容的 label 数据,鼠标悬浮显示超长内容
12 3
|
3天前
Element UI 多选表格--判断勾选数据行的 Checkbox 时为选中还是取消选中
Element UI 多选表格--判断勾选数据行的 Checkbox 时为选中还是取消选中
11 1
|
3天前
|
前端开发 JavaScript
零基础学 Vue + Element UI 第01步 —— 搭建开发环境、创建项目、修改默认模板、启动项目、访问项目
零基础学 Vue + Element UI 第01步 —— 搭建开发环境、创建项目、修改默认模板、启动项目、访问项目
10 1
|
4天前
|
JavaScript
vue + element UI【实战】音乐播放器/语音条(内含音频的加载、控制,事件监听,信息获取,手机网页阴影的去除等技巧)
vue + element UI【实战】音乐播放器/语音条(内含音频的加载、控制,事件监听,信息获取,手机网页阴影的去除等技巧)
7 1
|
3天前
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
5 0
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
|
4天前
|
前端开发 JavaScript
vue + element-UI 图片压缩canvas【详解】(含完整demo)
vue + element-UI 图片压缩canvas【详解】(含完整demo)
8 1
|
5天前
|
JavaScript
element-ui 在vue中el-input输入框的autofocus属性失效【解决方案】
element-ui 在vue中el-input输入框的autofocus属性失效【解决方案】
12 1