Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

スタンザタグの属性値を複数変更すると、更新イベントが複数回発生する #48

Open
okbp opened this issue Feb 3, 2016 · 0 comments

Comments

@okbp
Copy link
Contributor

okbp commented Feb 3, 2016

あるスタンザから別のスタンザの内容を更新する機能(スタンザの相互連携機能)の実現で、スタンザタグの属性値を変更する方法をncgcさんに教えていただいたのですが、
nsgc/ts_provider@master...update-actions
この方法だと、複数のパラメータを変更したい場合に、スタンザ更新イベントも複数回発生してしまい、意図しない結果になりました。

属性値の更新
//値が変更されている個数分だけイベントが発生する
$("togostanza-protein_compound").attr({
         'up_id': params.up_id,
         'standard_type': standard_type});

そこで属性変更ではなく、スタンザタグを置換(削除&作成)する方法を考えましたが、こちらもcreateElementで要素を組み立てる段階及び各属性追加の度にイベントが発生しました。

スタンザタグの置換(createElementでの作成)
//createElement, setAttribute毎に、計3回イベントが発生する
var stanza_elem = element = document.createElement('togostanza-protein_compound');
stanza_elem.setAttribute('up_id', params.up_id);
stanza_elem.setAttribute('standard_type', standard_type);
//DOM追加処理(省略)

最終的にスタンザタグをHTMLテキストで編集してDOMのイベントを一回だけに抑えると、スタンザ更新イベントも一回だけしか発生しませんでした。

スタンザタグの置換(HTMLテキストでの追加)
var stanza_elem = '<togostanza-protein_compound up_id="' + up_id + '" standard_type="' + standard_type + '"'></togostanza-protein_compound>'
//置換
var current_elem = $("togostanza-protein_compound")
current_elem.before(stanza_elem);
current_elem.remove();

一つの属性値変更であれば問題ないのですが、複数指定した場合に予期しない挙動になるので(気付きにくい)、特に決まった用途がなければ属性変更によるスタンザ更新イベントは無くてもいいかと思いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant