-
Notifications
You must be signed in to change notification settings - Fork 42
/
client.js
129 lines (118 loc) · 3.37 KB
/
client.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
angular.module("meteor", []).service("MeteorCollections", [
function() {
var self = this;
self.collections = {};
self.getCollection = function(name) {
if (self.collections[name]) {
return self.collections[name];
} else {
self.collections[name] = new Meteor.Collection(name);
return self.collections[name];
}
}
return self;
}]).factory("$meteorObject", [
function() {
function ObjectFactory(collection, objectvalue) {
if(!objectvalue){return;}
objectvalue.save = function() {
objectvalue = angular.copy(objectvalue);
cleanupAngularObject(objectvalue);
collection.update({
_id : this._id
}, objectvalue);
}
objectvalue.remove = function() {
collection.remove({
_id : this._id
});
}
return objectvalue;
}
return ObjectFactory;
}]).factory("$meteor", ["$rootScope", "MeteorCollections", "$meteorObject",
function($rootScope, MeteorCollections, $meteorObject) {
$rootScope.apply = _.debounce(function() {
try {
$rootScope.$digest();
} catch(e) {
setTimeout($rootScope.apply, 0);
}
}, 100);
function CollectionFactory(collection) {
var collection = MeteorCollections.getCollection(collection);
var value = [];
function Collection(value) {
value = {};
}
Collection.observe = function(cursor, array) {
cursor.observe({
"addedAt" : function(document, atIndex, before) {
//console.log(document);
if (!array) {
value = new $meteorObject(collection, document);
}
if (array) {
value[atIndex] = new $meteorObject(collection, document);
}
$rootScope.apply();
},
"changedAt" : function(newDocument, oldDocument, atIndex) {
value[atIndex] = new $meteorObject(collection, newDocument);
$rootScope.apply();
},
"removedAt" : function(oldDocument, atIndex) {
value.splice(atIndex, 1);
$rootScope.apply();
}
})
}
Collection.find = function(selector, options, callback) {
value = this instanceof Collection ? this : [];
this.observe(collection.find(selector, options), true);
return value;
}
Collection.findOne = function(selector, options, callback) {
value = this instanceof Collection ? this : {};
value = new $meteorObject(collection,collection.find(selector,options).fetch()[0]);
this.observe(collection.find(selector, options), false);
return value;
}
Collection.insert = function(values) {
values = angular.copy(values);
cleanupAngularObject(values);
return collection.insert(values);
}
Collection.update = function(selector, updateValues) {
updateValues = angular.copy(updateValues);
cleanupAngularObject(updateValues);
delete updateValues._id;
return collection.update(selector, {
$set : updateValues
});
}
Collection.remove = function(selector) {
return collection.remove(selector);
}
return Collection;
}
return CollectionFactory;
}]);
/** Removes AngularJS transient properties from Object tree */
cleanupAngularObject = function(value) {
if (value instanceof Array) {
for (var i = 0; i < value.length; i++) {
cleanupAngularObject(value[i]);
}
}
else if (value instanceof Object) {
for (property in value) {
if (/^\$+/.test(property)) {
delete value[property];
}
else {
cleanupAngularObject(value[property]);
}
}
}
}