JSFiddle - React, Tailwind, and code Playground
by Jeremy Likness
JavaScript
"use strict";
function Log () {
console.log('Log created.');
}
Log.prototype.log = function (msg) { console.log(msg); };
function CollectionHelper() {
return {
find: function (arr, id) {
var idx;
for (idx = 0; idx < arr.length; idx += 1) {
if (arr[idx].id === id) {
return arr[idx];
}
}
return null;
}
};
}
function Instructors (log, ch) {
this.log = log;
this.helper = ch;
this.instructors = [
{ id: 1, name: 'Jeremy Likness' }
];
log.log("Instructors created.");
}
Instructors.prototype.getInstructor = function (id) {
return this.helper.find(this.instructors, id);
};
function Courses (log, helper) {
this.log = log;
this.helper = helper;
this.courses = [
{ id: 1, name: 'Javascript Dependency Injection' }
];
log.log("Courses created.");
}
Courses.prototype.getCourse = function (id) {
return this.helper.find(this.courses, id);
};
function CourseMap(log, i, c) {
this.log = log;
this.instructors = i;
this.courses = c;
this.courseMap = [
{ instructorId: 1, courseId: 1 }
];
}
CourseMap.prototype.getCourses = function () {
var result = [], idx;
for (idx = 0; idx < this.courseMap.length; idx += 1) {
var map = this.courseMap[idx];
result.push( {
instructor: this.instructors.getInstructor(map.instructorId),
course: this.courses.getCourse(map.courseId)
});
}
return result;
}
CourseMap.$$deps = ['log', 'instructors', 'courses'];
function main () {
var ioc = new $$jsInject();
ioc.register('courseMap', [CourseMap]);
ioc.register('instructors', ['log', 'ch', Instructors]);
ioc.register('courses', ['log', 'ch', Courses]);
ioc.register('log', [Log]);
ioc.register('ch', [CollectionHelper]);
ioc.get('log').log(ioc.get('courseMap').getCourses()); ...