-
Notifications
You must be signed in to change notification settings - Fork 10
/
delete-button.js
65 lines (63 loc) · 2.03 KB
/
delete-button.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
Template.quickRemoveButton.helpers({
atts: function () {
var context = this, atts = {};
for (var prop in context) {
if (context.hasOwnProperty(prop) &&
prop !== "_id" &&
prop !== "collection" &&
prop !== "onError" &&
prop !== "onSuccess" &&
prop !== "beforeRemove") {
atts[prop] = context[prop];
}
}
if (!atts.type) {
atts.type = "button";
}
return atts;
}
});
Template.quickRemoveButton.events({
'click button': function (event, template) {
var self = this;
var collection = lookup(self.collection);
if (typeof Meteor !== "undefined" && Meteor.Collection) {
if (!(collection instanceof Meteor.Collection)) {
throw new Error("quickRemoveButton: collection attribute must be set to a Meteor.Collection instance or a string reference to a Meteor.Collection instance that is in the window scope.");
}
} else if (typeof Mongo !== "undefined" && Mongo.Collection) {
if (!(collection instanceof Mongo.Collection)) {
throw new Error("quickRemoveButton: collection attribute must be set to a Mongo.Collection instance or a string reference to a Mongo.Collection instance that is in the window scope.");
}
}
var onError = self.onError || function (error) {
alert("Delete failed");
console.log(error);
};
var onSuccess = self.onSuccess || function () {};
var beforeRemove = self.beforeRemove || function () { this.remove(); };
beforeRemove.call({
remove: function () {
collection.remove(self._id, function (error, result) {
if (error) {
onError(error);
} else {
onSuccess(result);
}
});
}
}, collection, self._id);
}
});
function lookup(obj) {
var ref = window, arr;
if (typeof obj === "string") {
arr = obj.split(".");
while(arr.length && (ref = ref[arr.shift()]));
if (!ref) {
throw new Error(obj + " is not in the window scope");
}
return ref;
}
return obj;
}