ObservableObject: reflectValues()

プロパティの状態を更新します。

構文

observableObject.reflectValues(valueMap) => object

引数

  • valueMap: object

    更新するプロパティ名と値の組となるオブジェクトです。

返値: object

引数 valueMap の各プロパティに対して更新した結果の値を格納したオブジェクトです。 更新に成功したプロパティは更新結果の値となり、更新に失敗したプロパティは元の値となります。

ターゲットにプロパティ値を反映させない例です。

// 双方向を許可しない
const source = new ObservableObject({ output: "foo" }, false);

const target1 = Object.assign(Object.create(target_proto), { output: "" });
// 双方向を要求するが許可されていない
source.bindData(target1, true);

const target2 = Object.assign(Object.create(target_proto), { output: "" });
// 単方向を要求
source.bindData(target2, false);

// ターゲットは更新されない
target1.reflectValues({ output: "bar" });
// source.output  => foo
// target1.output => foo
// target2.output => foo

// ターゲットは更新されない
target2.reflectValues({ output: "bar" });
// source.output  => foo
// target1.output => foo
// target2.output => foo

// ターゲットが更新される
source.reflectValues({ output: "bar" });
// source.output  => bar
// target1.output => bar
// target2.output => bar

画面要素の値をソースに反映させる実装例です。

// ある AlierView element のプロパティ prop_str を取得します
let value = element[prop_str];

// プロパティ prop_str を再割り当てします
Object.defineProperty(element, prop_str, {
    get() {
        return value;
    },
    set(newValue) {
        if (value != newValue) {
            this.source.reflectValues({ [this.id]: value });
        }
    },
});

解説

引数 valueMap のプロパティの中で、バインディングソースの同期対象となっているプロパティが valueMap のプロパティ値に更新されます。

この関数の呼び出しによってバインディングターゲットが実装する reflectValues() が呼び出されます。