ProtoViewLogic: collectAttributes()

メソッド relateElements() で関連付けられた HTML 要素から指定のキーの属性値を収集します。

構文

protoViewLogic.collectAttributes() => object
protoViewLogic.collectAttributes(key) => object

引数

  • key: string | null

    収集対象の属性またはプロパティの名前です。

返値: object

収集されたオブジェクトです。プロパティ名は収集された Element のプロパティ名です。プロパティ値は引数 key によって異なります:

  • key が文字列の場合

    Element が持つ key と同名の属性またはプロパティの値です。

    • 同じ名前を持つ属性とプロパティがあった場合、プロパティの値が優先されます。
    • 要素が配列だった場合、各要素の属性やプロパティの値を成分に持つ配列が値として返却されます。
    • key に対応する属性もプロパティもなかった場合、代わりに null が値として設定されます。
  • keynull の場合

    Element 自身です。

const vl = new ProtoViewLogic();

const foo = document.createElement("textarea");
// 属性 attr を設定
foo.setAttribute("attr", "attr_value");

const bar = [
    document.createElement("input"),
    document.createElement("input"),
    document.createElement("input"),
];
// 属性 prop を設定
bar[0].setAttribute("prop", "prop_value_0");
bar[1].setAttribute("prop", "prop_value_1");
bar[2].setAttribute("prop", "prop_value_2");

// Element を関連付け
vl.relateElements({ foo, bar });

// 属性 attr を収集
console.log(vl.collectAttributes("attr"));
// => { foo: "attr_value", bar: [null, null, null] }

// 属性 prop を収集
console.log(vl.collectAttributes("prop"));
// => { foo: null, bar: ["prop_value_0", "prop_value_1", "prop_value_2"] }

解説

例えば vl: ProtoViewLogic にプロパティ vl.foo: Element および vl.bar: Array<Element> が存在し、vl.foo が属性 attr="attr_value" を持つ場合、 vl.collectAttributes("attr"){ foo: "attr_value", bar: [null, ...] } を返します。

また同様に vl.bar[i] がプロパティ vl.bar[0].prop = "prop_value_0", ... を持つ場合、 vl.collectAttributes("prop"){ foo: null, bar: ["prop_value_0", ...] } を返します。