SAP UI5和Vue的双向绑定比较
发布日期:2021-06-30 14:20:23 浏览次数:2 分类:技术文章

本文共 5269 字,大约阅读时间需要 17 分钟。

Recently when I do self study on Vue I find many articles in the internet with full of praise on ‘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: .

Now I will reuse the button created for my blog to recall UI5 two way data binding. I have a simply button defined in XML 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: .

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.

{

{ message }}

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:

{

{ message }}

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( e v e n t . t a r g e t . c o m p o s i n g ) r e t u r n ; m e s s a g e = event.target.composing)return;message= 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.

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

转载地址:https://jerry.blog.csdn.net/article/details/105947193 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SAP Cloud for Customer里一个Promise的实际应用场合
下一篇:SAP UI5和Angular里控制器(Controller)实现逻辑比较

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月12日 16时06分10秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章