Skip to content

Instantly share code, notes, and snippets.

@kotobukid
Created November 28, 2012 05:01
Show Gist options
  • Save kotobukid/4159124 to your computer and use it in GitHub Desktop.
Save kotobukid/4159124 to your computer and use it in GitHub Desktop.
TDの中にチェックボックスがある時、余白部分をクリックしたと同時にチェック状態がトグルし、任意のイベント(__change)をトリガーさせるような処理をしたい
(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));
@kotobukid
Copy link
Author

身も蓋もない事を言うと、イベントプロパゲーション/バブリングの勉強が足りてない。

@kotobukid
Copy link
Author

更新してみた。自分的にはちゃんと意図したとおりに機能しているように見える。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment