(function($){$(document).ready(function(){updateList().bind('updateListComplete',function(e,data){$("#list").html(data);});});functionupdateList(){/** Use local event dispatcher. */vardispatcher=$({});$.ajax({type:'GET',url:'http://example.com/things',success:function(data){dispatcher.trigger('updateListComplete',[data]);}});returndispatcher;}functionaddSomethingToList(thingToAdd){$.ajax({type:'POST',url:'http://example.com/things',data:thingToAdd,success:function(data){updateList().one('updateListComplete',function(){showMsg('Your something was added. Yay.');});}});}})(jQuery);
(function($){/** Use global event bus. */varevents=$({});$(document).ready(function(){events.bind('updateListComplete',function(e,data){$("#list").html(data);});updateList();});functionupdateList(){$.ajax({type:'GET',url:'http://example.com/things',success:function(data){events.trigger('updateListComplete',[data]);}});}functionaddSomethingToList(thingToAdd){$.ajax({type:'POST',url:'http://example.com/things',data:thingToAdd,success:function(data){events.one('updateListComplete',function(){showMsg('Your something was added. Yay.');});updateList();}});}})(jQuery);
I like Option 1 because it feels more jQueryish (with the chaining). However, I think Option 2 is cleaner and easier to understand.