openui5 icon indicating copy to clipboard operation
openui5 copied to clipboard

xml view expression binding behaves weirdly if one of the parameters returns undefined

Open geekflyer opened this issue 11 years ago • 8 comments

Found with: SAPUI5 1.28.1

take the following example:

var jsonModel = new sap.ui.model.json.JSONModel({
showButton: true
});
<Button 
           text="test"
           visible="{= ${myModel>/showButton} &amp;&amp; undefined)}"/>

As you can see the visible property of the Button is set by an expression binding. As the second parameter in the logical && is a literal undefined the expression should always evaluate to undefined which coerces to false, so the button should never be shown.

What happens instead is that the second parameter undefined does not get evaluated at all and the button still remains visible. Only If I set showButton to false then the button get's invisible.

This is of course just a simplified example. The general bug is that whenever a part of an expression returns undefined this part seems to be ignored for the determination of the eventual result (at least I can reproduce this for boolean expressions with the && operator).

If you need a live code example please tell me where I can put it.

Thanks and Best Regards Christian

geekflyer avatar Jul 06 '15 13:07 geekflyer

here's a live example in jsbin (using latest openui5beta):

http://jsbin.com/datoza/4/edit?html,output

geekflyer avatar Jul 06 '15 13:07 geekflyer

This IMHO is not a bug in the expression binding. Expression binding works straight forward here and should determine a value of "undefined" for the above binding. It explicitly doesn't do type coercion, nor does the UI5 runtime. Instead, data binding interprets "undefined" or "null" as "reset property to default value" which is true for most "visible" properties. And, BTW, this is also the reason for not implementing type coercion here. It would break the important "reset" feature of the data binding.

You would observe the same by calling oButton.setVisible(false).setVisible(undefined);

You could modify the expression so that it delivers the result that you expect ( (your expression) || false) , but I don't see that this should be fixed in the expression binding.

codeworrior avatar Jul 06 '15 13:07 codeworrior

ok, got it. Was still some gotcha to me and my colleagues. Is it somewhere documented that undefined resets the value to it's default?

geekflyer avatar Jul 06 '15 14:07 geekflyer

I fixed it btw in our case by doing !!{myModel>/potentiallyUndefinedProperty} which coerces undefined to false explicitly.

geekflyer avatar Jul 06 '15 14:07 geekflyer

That's indeed nicer and shorter :-)

To be honest, I had to search for the documentation first and the place might not be the most obvious one: ManagedObject.validateProperty:

    /** ...          
     * In case <code>null</code> or <code>undefined</code> is passed, the default value for 
     * this property is used as value. If no default value is defined for the property, the 
     * default value of the type of the property is used.
     */

codeworrior avatar Jul 06 '15 14:07 codeworrior

ok thanks :-)

Would be good if you can add this to a more obvious place in the guides and add also some example where undefined is passed to a setter.

geekflyer avatar Jul 06 '15 14:07 geekflyer

I've created a internal incident 1770164580. You can check its status on GitHub.

Shtilianova avatar Apr 12 '17 08:04 Shtilianova

Hi, the documentation is already adapted. Please see the API reference. BR, Jan

loginger avatar Apr 12 '17 10:04 loginger