Skip to content

Instantly share code, notes, and snippets.

@alecmunro
Created September 13, 2011 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alecmunro/1214446 to your computer and use it in GitHub Desktop.
Save alecmunro/1214446 to your computer and use it in GitHub Desktop.
Mediator Pattern example
CarList = function(){
this.cars = [];
};
CarList.prototype.add_car = function(car){
this.cars.push(car);
this.render();
};
/*
Some code here for managing rendering of the list of cars.
*/
CarAddForm = function(add_car_callback){
this.add_car_callback = add_car_callback;
};
CarAddForm.setup_events = function(){
var _this = this;
$("button.add").live("click", function(){
var car = new object();
car.name = $("input.car_name").value();
_this.add_car_callback(car);
_this.clear_form();
});
};
/*
Some code here for managing rendering of the add form.
*/
MainPage = function(){
var _this = this;
this.car_list = new CarList()
this.car_add_form = new CarAddForm(function(car){
_this.add_car_to_list(car);
});
};
MainPage.prototype.setup_events = function(){
this.car_list.setup_events();
this.car_add_form.setup_events();
};
MainPage.prototype.add_car_to_list = function(car){
this.car_list.add_car(car);
alert("Added to list: " + car.name);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment