本文介绍了具有双向绑定模式的复合绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用相同的 SimpleForm 来创建新对象和进行编辑.我试着这样做:

I try to use the same SimpleForm for creating a new object and for editing. I try to make it this way:

<Input value="{= !${/isNew} ? ${som>Id} : ${newModel>Id}" />

但绑定不在双向模式中.是否有可能实现双向绑定?

But the bindings are not in mode TwoWay. Is there a possibility to make it to TwoWay Binding?

推荐答案

在属性绑定中,使用 ...

In property binding, using ...

... 将绑定模式转换为 OneWay(或者甚至 OneTime 在表达式绑定中的 {:= 的情况下).它们都使用了 CompositeBinding 模块,可以通过以下方式观察:>

... turns the binding mode into OneWay (or even OneTime in case of {:= in Expression Binding). They all make use of the module CompositeBinding which can be observed with:

myInput.getBinding("value").getMetadata().getName() // "sap.ui.model.CompositeBinding"

大多数属性绑定与因此,复合绑定是OneWay.

Most of the property bindings withCompositeBinding are therefore OneWay.

解决方案:当在parts之外还分配了一个type,但是,属性绑定可以变成TwoWay.适当的类型应该是从抽象模块派生的类型 CompositeType(例如 货币).

Solution: When a type is assigned in addition to the parts, however, the property binding can become TwoWay. The appropriate type should be then a type derived from the abstract module CompositeType (e.g. Currency).

请参阅:https://embed.plnkr.co/0MVvfZ/?show=view%2FHome.view.xml,预览

在我们的例子中,我们可以创建一个复合类型,它包含三元运算所需的所有三个 parts;一种用于条件,一种用于情况(a),一种用于情况(b):

In our case, we could create a composite type which takes all three parts necessary for the ternary operation; one for the condition, one for the truthy case (a), and one for the falsy case (b):

<Input value="{
  parts: [
    '/condition',
    'a>/value',
    'b>/value'
  ],
  type: 'demo.model.type.Ternary'
}" />

实际的三元运算发生在类型定义中,它看起来像这样:

The actual ternary operation happens in the type definition which could look something like this:

sap.ui.define([
  "sap/ui/model/CompositeType"
], function(CompositeType) {
  "use strict";

  return CompositeType.extend("demo.model.type.Ternary", {
    constructor: function() {
      CompositeType.apply(this, arguments);
      this.bParseWithValues = true; // make 'parts' available in parseValue
    },

    /**
    * Displaying data from the right model (model -> view)
    */
    formatValue: parts => parts[0] ? parts[1] : parts[2],

    /**
    * Assigning entered value to the right model (view -> model)
    */
    parseValue: (enteredValue, stuff, parts) => parts[0] ? [
      parts[0],
      enteredValue,
      parts[2],
    ] : [
      parts[0],
      parts[1],
      enteredValue,
    ],

    validateValue: () => true // Nothing to validate here
  });
});


注意

  • 确保所有绑定 parts 启用了 TwoWay 模式:


    Note

    • Make sure that all binding parts have the mode TwoWay enabled:

      请注意,当绑定部分之一未处于双向模式时,复合绑定将被强制进入模式 OneWay.

    • 为了首先启用 CompositeBinding,compatVersion=edge"(或 bindingSyntax=complex")必须在引导程序配置.

      这篇关于具有双向绑定模式的复合绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:52
查看更多