-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetData.js
More file actions
39 lines (36 loc) · 890 Bytes
/
getData.js
File metadata and controls
39 lines (36 loc) · 890 Bytes
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
var listToArray = require('list-to-array');
/**
* Gets the data from an Item ready to be serialised for client-side use, as
* used by the React components
*/
function getData (item, fields) {
var data = {
id: item.id,
name: this.getDocumentName(item)
};
if (this.autokey) {
data[this.autokey.path] = item.get(this.autokey.path);
}
if (fields === undefined) {
fields = Object.keys(this.fields);
}
if (fields) {
if (typeof fields === 'string') {
fields = listToArray(fields);
}
if (!Array.isArray(fields)) {
throw new Error('List.getData: fields must be undefined, a string, or an array.');
}
data.fields = {};
fields.forEach(function (path) {
var field = this.fields[path];
if (field) {
data.fields[path] = field.getData(item);
} else {
data.fields[path] = item.get(path);
}
}, this);
}
return data;
}
module.exports = getData;