-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticleprovider-memory.js
More file actions
69 lines (57 loc) · 1.37 KB
/
Copy patharticleprovider-memory.js
File metadata and controls
69 lines (57 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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];
articles._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();
}
this.dummyData[this.dummyData.length] = article;
}
callback(null, articles);
};
// Cargar datos de "mentirosos"
new ArticleProvider().save([{
title: 'post one',
body: 'body one',
comments: [{
author: 'bob',
comment: 'I loveit'
},
{
author: 'Dave',
comment: 'This is rubbish'
}]
},
{
title: 'Post two',
body: 'Body two'
},
{
title: 'Post three',
body: 'Body three'
}], function(error, articles){});
exports.ArticleProvider = ArticleProvider;