forked from leancloud/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelation.js
More file actions
120 lines (109 loc) · 3.55 KB
/
Copy pathrelation.js
File metadata and controls
120 lines (109 loc) · 3.55 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* 每位工程师都有保持代码优雅的义务
* Each engineer has a duty to keep the code elegant
**/
'use strict';
var _ = require('underscore');
module.exports = function(AV) {
/**
* Creates a new Relation for the given parent object and key. This
* constructor should rarely be used directly, but rather created by
* AV.Object.relation.
* @param {AV.Object} parent The parent of this relation.
* @param {String} key The key for this relation on the parent.
* @see AV.Object#relation
* @class
*
* <p>
* A class that is used to access all of the children of a many-to-many
* relationship. Each instance of AV.Relation is associated with a
* particular parent object and key.
* </p>
*/
AV.Relation = function(parent, key) {
if (! _.isString(key)) {
throw new TypeError('key must be a string');
}
this.parent = parent;
this.key = key;
this.targetClassName = null;
};
/**
* Creates a query that can be used to query the parent objects in this relation.
* @param {String} parentClass The parent class or name.
* @param {String} relationKey The relation field key in parent.
* @param {AV.Object} child The child object.
* @return {AV.Query}
*/
AV.Relation.reverseQuery = function(parentClass, relationKey, child){
var query = new AV.Query(parentClass);
query.equalTo(relationKey, child._toPointer());
return query;
};
AV.Relation.prototype = {
/**
* Makes sure that this relation has the right parent and key.
*/
_ensureParentAndKey: function(parent, key) {
this.parent = this.parent || parent;
this.key = this.key || key;
if (this.parent !== parent) {
throw "Internal Error. Relation retrieved from two different Objects.";
}
if (this.key !== key) {
throw "Internal Error. Relation retrieved from two different keys.";
}
},
/**
* Adds a AV.Object or an array of AV.Objects to the relation.
* @param {} objects The item or items to add.
*/
add: function(objects) {
if (!_.isArray(objects)) {
objects = [objects];
}
var change = new AV.Op.Relation(objects, []);
this.parent.set(this.key, change);
this.targetClassName = change._targetClassName;
},
/**
* Removes a AV.Object or an array of AV.Objects from this relation.
* @param {} objects The item or items to remove.
*/
remove: function(objects) {
if (!_.isArray(objects)) {
objects = [objects];
}
var change = new AV.Op.Relation([], objects);
this.parent.set(this.key, change);
this.targetClassName = change._targetClassName;
},
/**
* Returns a JSON version of the object suitable for saving to disk.
* @return {Object}
*/
toJSON: function() {
return { "__type": "Relation", "className": this.targetClassName };
},
/**
* Returns a AV.Query that is limited to objects in this
* relation.
* @return {AV.Query}
*/
query: function() {
var targetClass;
var query;
if (!this.targetClassName) {
targetClass = AV.Object._getSubclass(this.parent.className);
query = new AV.Query(targetClass);
query._extraOptions.redirectClassNameForKey = this.key;
} else {
targetClass = AV.Object._getSubclass(this.targetClassName);
query = new AV.Query(targetClass);
}
query._addCondition("$relatedTo", "object", this.parent._toPointer());
query._addCondition("$relatedTo", "key", this.key);
return query;
}
};
};