Skip to content

Instantly share code, notes, and snippets.

@egonelbre
Created February 28, 2014 09:35
Show Gist options
  • Save egonelbre/9268118 to your computer and use it in GitHub Desktop.
Save egonelbre/9268118 to your computer and use it in GitHub Desktop.
AttendEvent = Context(
function(event, attendee) {
Event = event;
Attendee = attendee;
Attendee.attend();
}, {
Attendee: {
attend: function(){
this.attendedEvents.push(Event);
Event.trackAttendence();
}
},
Event: {
trackAttendence: function(){
this.attendees.push(Attendee);
}
}
});
event = { title: "Something", attendees: [] };
person = { name: "John", attendedEvents: [] };
AttendEvent(event, person);
log("First attendee: " + event.attendees[0].name);
log("First event: " + person.attendedEvents[0].title);
OrganizeEvent = Context(
function(organizer, schedule, event) {
Organizer = organizer;
Event = event;
Schedule = schedule;
Errors = [];
Organizer.scheduleEvent();
return Errors;
}, {
Organizer:{
scheduleEvent: function(){
if(Event.isValid()){
Schedule.add(Event);
}
}
},
Event: {
isValid: function(){
if(this.title == ""){
Errors.add("Title is required");
return false;
}
if(this.date == ""){
Errors.add("Date is required");
return false;
}
return true;
}
},
Errors: {
add: function(err){ this.push(err); }
},
Schedule: {}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment