forked from lvbreda/Meteor_angularjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
129 lines (120 loc) · 3.3 KB
/
client.js
File metadata and controls
129 lines (120 loc) · 3.3 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
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 {
Meteor.subscribe(name);
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 () {
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);
/** Removes AngularJS transient properties from Object tree */
function cleanupAngularObject(value) {
if (value instanceof Array) {
for (var i = 0; i < value.length; i++) {
cleanupAngularObject(value[i]);
}
} else if (value instanceof Object) {
for (var property in value) {
if (/^\$+/.test(property)) {
delete value[property];
} else {
cleanupAngularObject(value[property]);
}
}
}
}
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) {
_.extend(value, document);
}
if (array) {
value.splice(atIndex, 0 , new $meteorObject(collection, document));
}
$rootScope.apply();
},
"changedAt": function (newDocument, oldDocument, atIndex) {
if (array) {
value[atIndex] = new $meteorObject(collection, newDocument);
} else {
_.extend(value, newDocument);
}
$rootScope.apply();
},
"removedAt": function (oldDocument, atIndex) {
value.splice(atIndex, 1);
$rootScope.apply();
}
})
}
Collection.find = function (selector, options) {
value = this instanceof Collection ? this : [];
this.observe(collection.find(selector, options), true);
return value;
}
Collection.findOne = function (selector, options) {
var doc = collection.find(selector, options).fetch()[0];
if (!doc) doc = {};
value = new $meteorObject(collection, doc);
this.observe(collection.find(selector, options), false);
return value;
}
Collection.insert = function (values, callback) {
values = angular.copy(values);
cleanupAngularObject(values);
return collection.insert(values, callback);
}
Collection.update = function (selector, updateValues, callback) {
updateValues = angular.copy(updateValues);
cleanupAngularObject(updateValues);
delete updateValues._id;
return collection.update(selector, {
$set: updateValues
}, callback);
}
Collection.remove = function (selector, callback) {
return collection.remove(selector, callback);
}
return Collection;
}
return CollectionFactory;
}]);