Presenter
Presenter listens to what happens on the View (click, scroll, keyboard, back button etc) and on the Model (things are added, removed or modified) and it updates both view and model accordingly. This "middleman" explicitly defines how the user interface behaves. Here's one for the Todo app:
$(function() {
/ 1. Initialize /
// create a model instance
var model = new Todo(),
// Grab the HTML root of this view
root = $("#todo-list"),
// HTML template for a single todo- entty
template = $("#todo-tmpl").html(),
/ 2. Listen to user events /
// "clear completed" is clicked
$("#clear-completed").click(function() {
todo.remove("completed");
})
// "toggle all" is clicked
$("#toggle-all").click(function() {
todo.toggle(filter);
})
// ...
/ 3. Listen to model events /
// an entry was removed
todo.on("remove", function(items) {
$.each(items, function() {
$(this.id).remove()
})
// an entry was edited
}).on("edit", function(item) {
var el = $(item.id);
el.removeClass("editing");
$("label, .edit", el).text(item.name).val(item.name);
})
// ...
})
I have put all the logic inside a single presenter, but there can be multiple presenters performing a separate task. For example, I could have put all the logic for a single todo entry in a separate file.
Feel free to decide the role of each presenter that makes the most sense for your application. This can be based on the role on the UI (sidebar, account, header...) or based on functionality (login, join, create, remove...).
Presenter has all the user interface logic, expressed with jQuery.