-
-
Save krunalm/11ab8c3fdaae3824ac0be1f5ea1bd3f4 to your computer and use it in GitHub Desktop.
Mock CourseAPI for "Building Applications with React and Flux" on Pluralsight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
//This file is mocking a web API by hitting hard coded data. | |
var courses = require('./courseData').courses; | |
var _ = require('lodash'); | |
//This would be performed on the server in a real app. Just stubbing in. | |
var _generateId = function(course) { | |
return course.title.replace(' ', '-'); | |
}; | |
var _clone = function(item) { | |
return JSON.parse(JSON.stringify(item)); //return cloned copy so that the item is passed by value instead of by reference | |
}; | |
var CourseApi = { | |
getAllCourses: function() { | |
return _clone(courses); | |
}, | |
getCoursesById: function(id) { | |
var course = _.find(courses, {id: id}); | |
return _clone(course); | |
}, | |
saveCourse: function(course) { | |
console.log('Imagine saving course via AJAX call...'); | |
if (course.id) { | |
var existingCourseIndex = _.indexOf(courses, _.find(courses, {id: course.id})); | |
courses.splice(existingCourseIndex, 1, course); | |
} else { | |
//just simulating creation here. This data | |
//would be generated on the server in a real app. | |
course.id = _generateId(course); | |
courses.push(_clone(course)); | |
} | |
return course; | |
}, | |
deleteCourse: function(id) { | |
console.log('Imagine deleting course with id of ' + id + ' via AJAX call...'); | |
_.remove(courses, { id: id}); | |
} | |
}; | |
module.exports = CourseApi; |
Author
krunalm
commented
Feb 21, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment