sap.m.Button 的 getBinding(“text") 未触发 attachChange 回调 [英] attachChange callback not fired for getBinding("text") of sap.m.Button
问题描述
onInit(): function{
this._oSelectedTrainingsModel = new sap.ui.model.json.JSONModel([]);
this.getView().setModel(this._oSelectedTrainingsModel, "selectedTrainings");
},
onAfterRendering: function() {
this.getView().byId("ViewSelectionButton").getBinding("text").attachChange(this._onTextUpdated, this);
},
在 XML 视图中,我有这个绑定:
In the XML view I have this binding:
<Button id="ViewSelectionButton"
class="magnificationPrep"
icon="sap-icon://cart-full"
tooltip="Share"
text="{i18n>viewTrainingsButton} ({= %{selectedTrainings>/}.length })"
press="handlePressViewSelection"
/>
在控制器中的某个点,一个对象被添加到_oSelectedTrainingsModel
:
At some point in the controller, an object is added to the _oSelectedTrainingsModel
:
this._oSelectedTrainingsModel.getProperty("/").push(oNewSelection);
此时,绑定的文本属性已更改,因此我期待调用回调函数 _onTextUpdated
.
At this point, the bound text property is changed and thus I'm expecting a call to the callback function _onTextUpdated
.
模型为空,这可能是问题的一部分?
The model is empty, this is probably part of the issue?
推荐答案
我相信由于您绑定模型和 Button 的 Text 属性的方式,不会触发更改事件.
I believe change event is not triggered because of the way you have bound your model and Text property of Button.
由于表达式绑定,发生了 ONE-WAY-BINDING,这意味着值将仅在 MODEL 中更改,不会反映在 UI 中.见下图:
Due to expression binding, ONE-WAY-BINDING has occured, meaning value will be changed in MODEL only and will not be reflected back in UI. See Image below:
为了解决这个问题,我手动触发了一个刷新事件,它会更新所有的绑定(强制),因此触发了按钮文本的更改事件.
To Solve the problem, I manually triggered an refresh event, which will update all the binding(forcefully) and hence, change event of Button Text was triggered.
所以,代码:
this.getView().getModel('newModel').getProperty("/").push({});
this.getView().getModel('newModel').refresh(true);
其中 newModel 是我命名的模型.
where newModel is my named model.
然而,在调试时,我发现了两个问题::(
However, while debugging, I found two problems: :(
为什么手动刷新后更改事件会触发两次?
Why is change event triggered twice after manual refresh ?
如果我这样做,不应该触发 onChange():
Shouldn't onChange() be triggered if I do:
this.byId('btn2').setText('Hey1!!')
?但事实并非如此.
希望能帮到你.
这篇关于sap.m.Button 的 getBinding(“text") 未触发 attachChange 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!