Angular DI Part 1: Service
by Jeremy Likness
JavaScript
function Log() {
console.log("Log created.");
this.log = function(msg) {
console.log(msg);
};
}
function Instructors() {
var log = new Log(),
_this = this,
instructors = [
{ id: 1, name: "Jeremy Likness" },
{ id: 2, name: "Jeffrey Richter" },
{ id: 3, name: "Jeff Prosise" }
];
log.log("Instructors created.");
this.getInstructors = function() {
return instructors.slice(0);
};
this.getInstructor = function(id) {
var list = _this.getInstructors(),
idx;
for (idx = 0; idx < list.length; idx += 1) {
if (list[idx].id === id) {
return list[idx];
}
}
return null;
};
}
function Courses() {
var log = new Log(),
_this = this,
courses = [
{ id: 1, description: "Threading" },
{ id: 2, description: "Windows 8" },
{ id: 3, description: "Angular" }
];
log.log("Courses created.");
this.getCourses = function() {
return courses.slice(0);
};
this.getCourse = function(id) {
var list = _this.getCourses(),
idx;
for (idx = 0; idx < list.length; idx += 1) {
if (list[idx].id === id) {
return list[idx];
}
}
return null;
};
}
function CourseMap() {
var log = new Log(),
_this = this,
courseMap = [
{ instructorId: 1, courseId: 3 },
{ instructorId: 2, courseId: 1 },
{ instructorId: 3, courseId: 2 }
];
this.instructors = new Instructors();
this.courses = new Courses();
log.log("CourseMap created.");
this.getCourseMap = function() {
var result = [],
idx;
for(idx = 0; idx < courseMap.length; idx += 1) {
result.push({
instructor: _this.instructors.getInstructor(courseMap[idx].instructorId),
...