I’m following this tutorial about building a blog with node.js, express and mongodb.
The following are functions that control articles submission (I think).
Of providers and data
Because the intention of this article is to show how one might use a
persistent approach in node.js we shall start with an abstraction:
provider. These ‘providers’ are going to responsible for returning and
updating the data. Initially we’ll create a dummy in-memory version
just to bootstrap us up and running, but then we’ll move over to using
a real persistence layer without changing the calling code.
articleprovider-memory.js:
var articleCounter = 1;
ArticleProvider = function(){};
ArticleProvider.prototype.dummyData = [];
ArticleProvider.prototype.findAll = function(callback) {
callback( null, this.dummyData )
};
ArticleProvider.prototype.findById = function(id, callback) {
var result = null;
for(var i =0;i<this.dummyData.length;i++) {
if( this.dummyData[i]._id == id ) {
result = this.dummyData[i];
break;
}
}
callback(null, result);
};
ArticleProvider.prototype.save = function(articles, callback) {
var article = null;
if( typeof(articles.length)=="undefined")
articles = [articles];
for( var i =0;i< articles.length;i++ ) {
article = articles[i];
article._id = articleCounter++;
article.created_at = new Date();
if( article.comments === undefined )
article.comments = [];
for(var j =0;j< article.comments.length; j++) {
article.comments[j].created_at = new Date();
}
Can anyone explain to me in simple English what each function is doing (sorry I’m a JavaScript beginner)?
(by the way, it is because of any common practice that the author decided to only start with a CAP in ArticleProvider?)
This file is a class definition for ArticleProvider with various instance methods.
Defining article provider as function and then using prototype to define further functions
of ‘findAll’, ‘findById’, and ‘save’, means that you can call these functions using the
syntax:
articleCounter is a variable locally available to the file containing
ArticleProvider definition
dummyData is an internal variable available to an ArticleProvider object
ArticleProvider.findAll(callback)
Will invoke callback(array) with all articles currently saved to ArticleProvider.
Caller must have defined a callback function that can accept one variable, and expect
the variable to be filled with an array of articles when called
ArticleProvider.findById(id, callback)
Will invoke callback(article) with a single article that matches the ‘id’ provided
in the parameter. Again, callback is defined by caller and accepts a single parameter
which will be the article found
ArticleProvider.save(articles,callback)
What you’ve listed here is truncated so I only have a partial answer for this function:
Will accept an array of articles, and set various fields in the submitted articles. These
fields include the article id, article creation date, article comments, and comment creation
dates
I’m guessing that in the code not listed these articles are saved to
ArticleProvider.dummyData, and that the callback is called with some parameter.