Created
November 28, 2012 05:01
-
-
Save kotobukid/4159124 to your computer and use it in GitHub Desktop.
TDの中にチェックボックスがある時、余白部分をクリックしたと同時にチェック状態がトグルし、任意のイベント(__change)をトリガーさせるような処理をしたい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
"use strict"; | |
// チェックボックスと、それを包むTDのクリックイベントを連動させるモジュール。 | |
var TdInputBind = window.Backbone.View.extend({ | |
initialize: function (options) { | |
var settings = _.extend({el: 'table'}, options || {}); | |
this.setElement(settings.el); | |
}, | |
events: { | |
'click td:has("input")': 'tdClicked', | |
'click input': 'inputChanged' | |
}, | |
tdClicked: function (e) { | |
var checked = $('input:checkbox', e.currentTarget).get(0).checked; | |
$('input:checkbox', e.currentTarget).get(0).checked = !checked; | |
//ここでcheckedが変化する | |
$(e.currentTarget).trigger('__change'); //変化した後のcheckedを任意のイベントで使用 | |
// 名前をmangleしてみる | |
}, | |
inputChanged: function (e) { | |
//ここでcheckedが変化する | |
e.stopPropagation(); | |
var checked = $(e.currentTarget).get(0).checked; | |
$(e.currentTarget).closest('td').trigger('__change'); | |
} | |
}); | |
Backbone.TdInputBind = TdInputBind; | |
}(jQuery)); |
更新してみた。自分的にはちゃんと意図したとおりに機能しているように見える。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
身も蓋もない事を言うと、イベントプロパゲーション/バブリングの勉強が足りてない。