具有功能的OpenUI5绑定属性,而不是直接访问 [英] OpenUI5 binding property with a function, instead of direct access
问题描述
我想将JSONModel的属性(在这种情况下为flag_baz)绑定到复选框。
事情是json模型看起来像这样。
I would like to bind a property (flag_baz in this case) from a JSONModel to a checkbox. Thing is that the json model looks like this.
{
foo: "Foo",
bar:"Bar",
flag_baz : "X"
}
在这种情况下,X表示 true,而空字符串表示 false
in this case X means "true" and an empty string means "false"
我想做的是评估一个将模型绑定到复选框的函数(会将 X /转换为true / false),并在从复选框绑定到模型时评估其他功能(将从true / false转换回 X /\"\")。
What i would like to do is evaluate a function for binding from model to the checkbox (that would translate "X"/"" to true/false) and evaluate some other function when binding from the checkbox to the model (that would translate from true/false back to "X"/"").
我想要这样的东西:
var checkBox = new Checkbox();
checkBox.bindProperty("checked", "flag_baz", funcFromStringToBool, funcFromBoolToString);
我知道funcFromStringToBool被称为格式化程序。
i know the funcFromStringToBool is called a formatter.
我将如何添加funcFromBoolToString函数?
how would i add the funcFromBoolToString function?
希望这很有道理。
Thx提前。
推荐答案
好,以防万一,我自己找到了答案。
Well in case some cares i've found the answer on my own.
所有绑定都可以使用类似的类型
All bindings can use a type like so
checkBox.bindProperty("checked", {
path : "flag_baz",
type : new BooleanStringType()
});
BooleanStringType类如下:
the BooleanStringType class would look like this:
sap.ui.model.SimpleType.extend("BooleanStringType", {
//called when going from model to ui
formatValue : function(flag_baz){
return flag_baz === "X";
},
//called when going from ui back to the model
parseValue : function(flag_baz){
return flag_baz ? "X" : "";
},
validateValue : function(flag_baz){
//some validation if needed
}
});
这篇关于具有功能的OpenUI5绑定属性,而不是直接访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!