-
Notifications
You must be signed in to change notification settings - Fork 5
/
document-methods.js
55 lines (55 loc) · 2.31 KB
/
document-methods.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
Meteor.addCollectionExtension(function () {
var inst = this;
if ((!_.isFunction(inst._transform) || inst._hasCollectionHelpers || inst._helpers) && _.isFunction(inst.helpers)) {
inst.helpers({
$save: function () {
var doc = {};
_.each(this, function (value, key) {
doc[key] = value;
});
delete doc._id;
// I would _.clone or _.omit, but for some reason
// freaking underscore.js copies the prototypes
return inst.update.apply(inst, _.flatten([this._id, { $set: doc }, _.toArray(arguments)]));
},
$duplicate: function () {
var doc = {};
_.each(this, function (value, key) {
doc[key] = value;
});
delete doc._id;
return inst.insert.apply(inst, _.flatten([doc, _.toArray(arguments)]));
},
$update: function (modifier) {
return inst.update.apply(inst, _.flatten([this._id, modifier, _.rest(_.toArray(arguments))]));
},
$remove: function () {
return inst.remove.apply(inst, _.flatten([this._id, _.toArray(arguments)]));
},
$set: function (toSet) {
return inst.update.apply(inst, _.flatten([this._id, { $set: toSet }, _.rest(_.toArray(arguments))]));
},
$unset: function (toSet) {
return inst.update.apply(inst, _.flatten([this._id, { $unset: toSet }, _.rest(_.toArray(arguments))]));
},
$push: function (toPush) {
return inst.update.apply(inst, _.flatten([this._id, { $push: toPush }, _.rest(_.toArray(arguments))]));
},
$pushAll: function (toPush) {
return inst.update.apply(inst, _.flatten([this._id, { $pushAll: toPush }, _.rest(_.toArray(arguments))]));
},
$pull: function (toPull) {
return inst.update.apply(inst, _.flatten([this._id, { $pull: toPull }, _.rest(_.toArray(arguments))]));
},
$pullAll: function (toPull) {
return inst.update.apply(inst, _.flatten([this._id, { $pullAll: toPull }, _.rest(_.toArray(arguments))]));
},
$pop: function (toPop) {
return inst.update.apply(inst, _.flatten([this._id, { $pop: toPop }, _.rest(_.toArray(arguments))]));
},
$addToSet: function (toAdd) {
return inst.update.apply(inst, _.flatten([this._id, { $addToSet: toAdd }, _.rest(_.toArray(arguments))]));
}
});
}
});