3
votes

Is it possible to bind control property to model with dynamic property name, stored, for instance, within another model field? I thought we can use SAPUI5 Expression Binding for this purpose, but it doesn't work: binding in trace window is broken and expression seems to be not evaluated at all.

XML View

<TextArea value="{= ${StackOverflow>/bindTextAreaTo} }" />

Controller

oModel = this.getView().getModel("StackOverflow");

/*
 * The model have two properties: question and comment
 * I want value of TextArea to be bound to one of them based on some condition
 */

oModel.setProperty("/question", "");
oModel.setProperty("/comment", "");

oModel.setProperty("/bindTextAreaTo", 
    bAsk ? "StackOverflow>/question" : "StackOverflow>/comment" );
1

1 Answers

7
votes

No, that's currently not possible.

However, there is a simple workaround for what you want to do (see below). Basically, you create a view model and set some boolean value on the model. This flag is then used in your expression binding to define "dynamically" what property of what model shall be used...

XMLView

<TextArea value="{= ${view>/ask} ? ${StackOverflow>/question} : ${StackOverflow>/comment} }" />

Controller

var oModel = this.getView().getModel("StackOverflow");
oModel.setProperty("/question", "");
oModel.setProperty("/comment", "");
//...

var oViewModel = new sap.ui.model.json.JSONModel();
this.getView().setModel(oViewModel, "view);
//...

oViewModel.setProperty("/ask", bAsk);