2012-05-07 7 views
1

jQueryとbackbone.jsを使用して、JavaScriptとHTMLを可能な限り分離したいと思います。
custom attributeを追加すると、JavaScriptとHTMLの分離を改善できますか? そして、新しいjQueryセレクターを使用するにはどうすればいいですか?JavaScriptとHTMLのより良い分離のためのカスタム属性の追加

例:あなたが使用することができます

<input type="text" id="todo-input" class="todo-input" tid="tid-attribute" > hello world </input> 

// The DOM events 
// I would like to use another selector tid and not class or id 
events: { 
    "keypress #todo-input" : "updateOnEnter" // I would like to use another selector tid not id, 
    "keypress .todo-input" : "updateOnEnter" // I would like to use another selector tid and not class 
} 
+0

私はまだそれを得ることはありません。分離したいですか?または属性が気にします。限り、私はこれがビューの仕事だと言うことができる限り、バックボーンアプリが依存するサーバーから何もレンダリングしないでください。これにより、バックボーンアプリがどこにでも展開されます。 – Deeptechtons

+0

HTMLの 'class'属性は、CSSを使ったスタイリングだけでなく、あらゆる種類のものに使われます。セレクタで使用するために要素に余分なクラスを追加することには何も問題ありません。 –

答えて

3

HTML5はdata-で始まるカスタムデータ属性が可能になります:

<input type="text" data-custom-attr="somevalue"> 

は、その後、あなたの基幹イベント宣言で:

"keypress [data-custom-attr='somevalue']" : "updateOnEnter" 
0

$('input[type="text"]').attr('tid', 'tid-attribute'); 

OR

$('input[type="text"]').attr('data-tid', 'tid-attribute'); 
関連する問題