-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelationship.js
More file actions
40 lines (38 loc) · 933 Bytes
/
relationship.js
File metadata and controls
40 lines (38 loc) · 933 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
40
var keystone = require('../../');
var utils = require('keystone-utils');
/**
* Registers relationships to this list defined on others
*/
function relationship (def) {
if (arguments.length > 1) {
for (var i = 0; i < arguments.length; i++) {
this.relationship(arguments[i]);
}
return this;
}
if ('string' === typeof def) {
def = { ref: def };
}
if (!def.ref) {
throw new Error('List Relationships must be specified with an object containing ref (' + this.key + ')');
}
if (!def.refPath) {
def.refPath = utils.downcase(this.key);
}
if (!def.path) {
def.path = utils.keyToProperty(def.ref, true);
}
Object.defineProperty(def, 'refList', {
get: function() {
return keystone.list(def.ref);
}
});
Object.defineProperty(def, 'isValid', {
get: function() {
return keystone.list(def.ref) ? true : false;
}
});
this.relationships[def.path] = def;
return this;
}
module.exports = relationship;