= function(name) { var item = { ... } items[item.id] = item; this.trigger("add", item); } this.remove = function(filter) { var els = this.items(filter); $.each(els, function() { delete items[this.id] }) this.trigger("remove", els); } $.observable(this); } $(function() { / 1. Initialize / var todo = new Todo(); / 2. Listen to user events / $("#new-‐todo").keyup(function(e) { var val = $.trim(this.value); if (e.which == 13 && val) { todo.add(val); this.value = ""; } }); $("#clear-‐completed").click(function() { todo.remove("completed"); }) // ... / 3. Listen to model events / // an entry was edited todo.on("add", add).on("remove", function(items) { $.each(items, function() { $("#" + this.id).remove() }); }); // ... }) ࣄ݅ܥ౷ / Observer pattern trigger(eventName, args...) on(eventName, fn) off(eventName) 13年11⽉月30⽇日星期六