Show TOC

Step 5: One-Way Data BindingLocate this document in the navigation structure

In contrast to the two-way binding behavior shown above, one-way data binding is also possible. Here, data is transported in one direction only: from the model, through the binding instance to the consumer (usually the property of a control), but never in the other direction. In this example, we will change the previous example to use one-way data binding. This will illustrate how the flow of data from the user interface back to the model can be switched off if required.

Preview
Figure 1: Two-way data binding disabled for the checkbox
Coding

You can view and download all files in the Explored app in the Demo Kit under Data Binding - Step 5.

webapp/index.html
...
    <script>
      var oModel = new sap.ui.model.json.JSONModel({
        firstName : "Harry",
        lastName  : "Hawk",
        enabled   : true,
        panelHeaderText : "Data Binding Basics"
      });
      oModel.setDefaultBindingMode(sap.ui.model.BindingMode.OneWay);
      // Assign the model object to the SAPUI5 core
      sap.ui.getCore().setModel(oModel);
...

Insert the single highlighted line immediately after the creation of the model object in index.html.

Now, no matter what state the checkbox is in, the input fields remain open for input because one-way data binding ensures that data flows only from the model to the UI, but never in the other direction.

The binding mode (one-way or two-way) is set on the model itself. Therefore, unless you specifically alter it, a binding instance will always be created using the model's default binding mode.

Should you wish to alter the binding mode, then there are two ways of doing this:
  • Alter the model's default binding mode. This is the approach used above.

  • Specify the data binding mode for a specific binding instance by using the oBindingInfo.mode parameter. This change applies only to this data binding instance. Any other binding instances will continue to use the model's default binding mode. For more information, see the API Reference for sap.ui.base.ManagedObject.bindProperty in the Demo Kit.

Note
There are two important points to understand about alterations to a model object's data binding mode:
  • If you alter the default binding mode of a model (as in the example above), then unless you explicitly say otherwise, all binding instances created after that point in time will use the altered binding mode.

  • Altering a model's default binding mode has no effect on already existing binding instances.