私は、典型的な関数呼び出しプログラミング構造の代わりに、jQuery のイベント (bind() および trigger()) を使用する方向にシフトすることでメリットが得られると思われるプログラムを作成しました。たとえば、次の代わりに:
//device changed
//(diffs holds key: {old_val: 'old value', new_val: 'new value'} pairs)
DEVICES.update(device, diffs)
DIAGRAM.update(device, diffs)
TABLES.update(DEVICES.$table, device, diffs)
/*
...
*/
//device changed - some other code location
DEVICES.update(device, diffs);
DIAGRAM.update(device, diffs);
TABLES.update(DEVICES.$table, device, diffs);
私は書くことができます:
//DEVICES.update(), DIAGRAM.update, and TABLES.update have all been bound to
//DEVICES.$container's device_changed custom event (in their respective modules)
//device changed
//(diffs holds key: {old_val: 'old value', new_val: 'new value'} pairs)
DEVICES.$container.trigger('device_changed', [device, diffs]);
/*
...
*/
//device changed - some other code location
DEVICES.$container.trigger('device_changed', [device, diffs]);
bind() と trigger() を効果的に使用する方法を学んでいますが、ほとんどの場合、作業を進めていくだけです。jQuery を使用して、このイベント駆動型スタイルに従ってコードをプログラミング/構造化する方法の良い例はありますか?