OpenUI5 binding property with a function, instead of direct access

故事扮演 提交于 2019-12-13 04:47:11

问题


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"
}

in this case X means "true" and an empty string means "false"

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"/"").

i would like to have something like this:

var checkBox = new Checkbox();
checkBox.bindProperty("checked", "flag_baz", funcFromStringToBool, funcFromBoolToString);

i know the funcFromStringToBool is called a formatter.

how would i add the funcFromBoolToString function?

Hope this makes sense.

Thx in advance.


回答1:


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()
});

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
    }
});


来源:https://stackoverflow.com/questions/30323120/openui5-binding-property-with-a-function-instead-of-direct-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!