-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniqueObjects.js
159 lines (150 loc) · 6.69 KB
/
uniqueObjects.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Implementation of Unique Objects.
* Create a new object by:
* 1. var Testing = Parse.UniqueObject.extend("Testing", {_uniqueKey: 'uniqColumn'});
* 2. var Testing = Parse.UniqueObject("Testing", {uniqueKey:'entry'})
*
* To save a unique object by the uniqueKey column, simply run obj.save()
* If you want to save a list of object to save latency, use Parse.UniqueObject.saveAllUnique
* instead of Parse.Object.saveAll.
*
**/ (function () {
var tmpObj = {
loaded: function () {
Parse.UniqueObject = Parse.UniqueObject || _.extend(Parse.Object);
Parse.UniqueObject.prototype.initialize = function () {
if (!this._uniqueKey && this.attributes['uniqueKey']) {
this._uniqueKey = this.get('uniqueKey');
this.unset('uniqueKey');
}
if (this._uniqueKey) {
this.saveUnique = function () {
var args = Array.prototype.slice.call(arguments),
q = new Parse.Query(this.className),
options = args[args.length - 1];
if (args.length == 2 && args[0] !== null) {
this.set(args[0], args[1]);
} else if (args.length == 3) {
this.set(args[0], args[1], args[2]);
}
q.equalTo(this._uniqueKey, this.get(this._uniqueKey));
q.first({
success: function (result) {
if (!result) {
Parse.Object.save.apply(this, args);
} else {
if (options && options.exists && _.isFunction(options.exists)) {
options.exists.apply(this, arguments);
} else {
console.log("exists");
}
}
},
error: function (r) {
throw new Error("Could not check remote for object existence");
}
});
};
}
}
Parse.UniqueObject.saveAllUnique = function (aToSave, oOptions) {
var oldSave = Parse.Object.saveAll,
oClassContained = {},
aClassObjectToSave = [],
oUniqueClasses = {},
oClassKeys = {};
_.each(aToSave, function (obj, i) {
var sClass = obj.className;
if (obj._uniqueKey) {
var objUniqueValue = obj.get(obj._uniqueKey);
oClassKeys[sClass] ? true : oClassKeys[sClass] = obj._uniqueKey;
oClassContained[sClass] ? oClassContained[sClass].push(objUniqueValue) : oClassContained[sClass] = [objUniqueValue];
oUniqueClasses[sClass] ? oUniqueClasses[sClass][objUniqueValue] = obj : (oUniqueClasses[sClass] = {
objUniqueValue: obj
});
} else {
aClassObjectToSave.push(obj);
}
})
console.log("Grouped items for unique save", oClassKeys, oClassContained, aClassObjectToSave);
_.each(_.keys(oClassContained), function (sClass, i) {
var uniqueKey = oClassKeys[sClass],
queryObj = new Parse.Query(sClass);
queryObj.containedIn(uniqueKey, oClassContained[sClass]);
queryObj.find({
success: function (res) {
_.each(res, function (found, i) {
// attempt to update
var data = oClassContained[sClass][found.get(uniqueKey)];
if (data && data instanceof Parse.Object) {
found.save(data.toJSON(), {success:function(r){console.log(r.get(uniqueKey) + ' successfully updated')},error:function(r){console.log(r.get(uniqueKey) + ' was not updated')}});
}
delete(oClassContained[sClass][found.get(uniqueKey)]);
});
// call old saveAll on remaining objects.
oldSave.call(null, oClassContained[sClass], oOptions);
console.log("Would have called the old save here");
},
error: function (res) {
console.log("Error attempting to find " + oClassContained[sClass].join(',') + " in class " + sClass + " under column " + uniqueKey);
}
});
});
oldSave.call(null, aClassObjectToSave, oOptions);
}
function TestUniqueObject() {
var Testing = Parse.UniqueObject.extend("Testing", {
_uniqueKey: 'entry'
});
a = new Testing({
"entry": "doorway"
});
b = new Testing({
"entry": "alleyway"
});
c = new Testing({
"entry": "garage"
});
a.save();
a.save(null, {
exists: function () {
console.log("this already exists")
}
});
b.save({
"entry": "entryValue",
"foo": "bar"
}, {});
c.save('entry', 'entryValue', {});
d = new Testing({
"entry": "gangam style"
});
e = new Testing({
"entry": "blue ribbon"
});
Parse.UniqueObject.saveAllUnique([d, e], {
success: function (a) {
console.log("success: ", a);
},
error: function (b) {
console.log("error: ", b)
}
});
}
//TestUniqueObject();
},
watch: function () {
var that = this;
this.timer = setInterval(function () {
if (window.Parse) {
clearInterval(that.timer);
that.loaded();
} else {
console.log("Parse not loaded yet");
}
}, 100);
},
timer: null
}
tmpObj.watch()
})();