ProtoViewLogic: getRelationshipOf()

与えられた要素について、ProtoViewLogic との関係性を取得します。

要素が relateElements() 関数によって ProtoViewLogic と関連付けられている場合、その ProtoViewLogic の参照と、ProtoViewLogic が持つ要素への参照のプロパティ名と、もし要素の配列として関連付けられているならそのインデックスを取得できます。

構文

ProtoViewLogic.getRelationshipOf(element) => object

引数

  • element: Element

    ProtoViewLogic との関係性を調べる対象の要素です

返値: object

与えられた要素 element を関連付けている ProtoViewLogic の情報を表すオブジェクトです。

  • owner: ProtoViewLogic | null

    element を関連付けている ProtoViewLogic です

  • name: ProtoViewLogic | null

    関連付けている ProtoViewLogic が持つ element への参照のプロパティ名です

    • ownernull でない場合、o.owner[o.name]element そのものか、element を含む配列を指します(o は返値のオブジェクトとします
  • index: ProtoViewLogic | null

    関連付けている ProtoViewLogic が持つ要素の配列における element の位置を示すインデックスです

    • o.owner[o.name]element を含む配列である場合、o.owner[o.name][o.index]element そのものを指します(o は返値のオブジェクトとします

const vl    = new ProtoViewLogic();
const span  = document.createElement("span");
const spans = [ document.createElement("span") ];

//  関係性の取得; 空のオブジェクトが返る
console.log(ProtoViewLogic.getRelationshipOf(span));
//  ==> {}

//  vl と span を関連付ける
vl.relateElements({ span });

//  関係性の取得; owner, name が設定されたオブジェクトが返る
const spanInfo = ProtoViewLogic.getRelationshipOf(span);
console.log(spanInfo);
//  ==> { owner: vl, name: "span" }

//  整合性の確認
console.log(spanInfo.owner[spanInfo.name] === span);
//  ==> true

//  vl と spans を関連付ける
vl.relateElements({ spans });

//  関係性の取得; owner, name, index が設定されたオブジェクトが返る
const spansInfo = ProtoViewLogic.getRelationshipOf(spans[0]);
console.log(spansInfo);
//  ==> { owner: vl, name: "spans", index: 0 }

//  整合性の確認
console.log(spansInfo.owner[spansInfo.name][spansInfo.index] === spans[0]);
//  ==> true

解説

与えられた要素について、ProtoViewLogic との関係性を取得します。

引数 elementProtoViewLogic が関連付けられているなら { owner, name, index } が返ります。そうでないなら、空のオブジェクトを返します。また indexelement が配列成分として含まれている場合にのみ設定されます。