SAP UI5和Vue的双向绑定比较

简介:

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:

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

In controller’s onInit function, the text property is bound to field “field_for_text” in JSON model:

onInit: function(){
        var oModel = new sap.ui.model.json.JSONModel();
        var myData = {"field_for_text": "Jerry button label"};

        oModel.setData(myData);
        var button = this.getView().byId("jerryButton");
        button.setModel(oModel);
        button.bindProperty("text", "/field_for_text");
        button.oModel = oModel;
}

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

Type the following script in Console.
Before I press entry key, the field “field_for_text” is still the current label “Jerry button label” displayed in UI:

After I press enter key, the model field is changed as well.

The logic for the synchronization in this direction ( control -> model ) has already been explained in my blog: How I do self-study on a given Fiori control – part 7 UI5 binding mode.

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

Now let’s do the opposite.
After I change model field and press enter key in console, nothing happens.

In order to make the change on model field flows to control property, it is necessary to call refresh function of JSON model.

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


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

<html>
<script src="lib/vue.js"></script>
<script>
function init() {
jerry = new Vue({
  el: '#app',
  data: {
    message: 'Hello World'
  }
});
}
</script>
<body onload = "init();">
<div id="app">
  <p>{{ message }}</p>
</div>
</body>
</html>

Two 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”;

And type enter key, the UI will react accordingly:

The mechanism for this direction of synchronization is, when the Vue application is started, the compiled attribute is registered as a “reactive” field in function defineReactive:

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

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

In patchVnode function, old DOM node will be replaced with new DOM node created with modified attribute done in console:

In the end the new DOM element’s attribute textContent is filled with new value of reactive field and after that we can immediately see this latest value in UI:

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

I made some small changes on the example:

<html>
<script src="lib/vue.js"></script>
<script>
function init() {
jerry = new Vue({
  el: '#app',
  data: {
    message: 'Hello World'
  }
});
}
</script>
<body onload = "init();">
<div id="app">
  <p>{{ message }}</p>
  <input v-model="message"></input>
</div>
</body>
</html>

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

Step2 – Generate source code of event handler for input field based on extracted directive in previous step

It is this very line of generated code which writes back to the model field with user input:

if($event.target.composing)return;message=$event.target.value
The complete generated source code for directive “v-model=”message” could be found from below code.
A real JavaScript function is created dynamically with this generated source code as function body, which will be used as the concrete event handler for the physical DOM element.

Step3 – Register event handler into physical DOM element

The registration is done using native function addEventListener.

Step4 – Model field will be changed in onInput event handler

Now as long as you type something in input field, the event handler created in step 2 and registered in step 3 will capture this event and react accordingly.
Model field is changed now!

Summary

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.

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

相关文章
|
JavaScript 前端开发 编译器
揭密 Vue 的双向绑定
Vue 中需要输入什么内容的时候,自然会想到使用 <input v-model="xxx" /> 的方式来实现双向绑定。下面是一个最简单的示例 JsFiddle 演示 https://jsfiddle.net/0okxhc6f/ 在这个示例的输入框中输入的内容,会随后呈现出来。
1164 0
|
JavaScript 前端开发 编译器
揭密 Vue 的双向绑定
Vue 中需要输入什么内容的时候,自然会想到使用 的方式来实现双向绑定。下面是一个最简单的示例 剖析Vue原理&实现双向绑定MVVM What's your name: Hello {{ name }} new Vue({ el: "#app", data: { name: "" } }); JsFiddle 演示https://jsfiddle.net/0okxhc6f/ 在这个示例的输入框中输入的内容,会随后呈现出来。
928 0
|
1天前
|
数据采集 JavaScript 前端开发
Vue框架的优缺点是什么
【7月更文挑战第5天】 Vue框架:组件化开发利于重用与扩展,响应式数据绑定简化状态管理;学习曲线平缓,生态系统丰富,集成便捷,且具性能优化手段。缺点包括社区规模相对小,类型支持不足(Vue 3.x改善),路由和状态管理需额外配置,SEO支持有限。随着发展,部分缺点正被克服。
7 1
|
2天前
|
JavaScript
Vue卸载eslint的写法,单独安装eslint,单独卸载eslint
Vue卸载eslint的写法,单独安装eslint,单独卸载eslint
|
2天前
|
JavaScript
青戈大佬安装Vue,无Eslint安装版,vue2安装,vue2无eslint,最简单配置Vue安装资料
青戈大佬安装Vue,无Eslint安装版,vue2安装,vue2无eslint,最简单配置Vue安装资料
|
2天前
|
JavaScript 区块链
vue 自定义网页图标 favicon.ico 和 网页标题
vue 自定义网页图标 favicon.ico 和 网页标题
10 1
|
2天前
|
JavaScript
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run
|
3天前
|
存储 JavaScript 数据安全/隐私保护
vue实战——登录【详解】(含自适配全屏背景,记住账号--支持多账号,显隐密码切换,登录状态保持)
vue实战——登录【详解】(含自适配全屏背景,记住账号--支持多账号,显隐密码切换,登录状态保持)
12 1
|
1天前
|
JavaScript 前端开发 开发工具
如何学习vue框架
【7月更文挑战第5天】 - 先学HTML/CSS/JS基础和前端工程化工具(npm, webpack, Git)。 - 从Vue官方文档学习基础,包括指令、组件、响应式系统。 - 深入研究Vue Router和Vuex,掌握路由管理和状态管理。 - 学习自定义指令和Mixins,优化性能技巧。 - 实战项目练习,加入Vue社区,阅读相关资源,提升技能。 - 关注Vue生态,持续实践和创新,以适应不断发展的框架。
5 0
|
2天前
|
缓存 JavaScript 算法
vue 性能优化
vue 性能优化
11 0