Skip to content

Commit ad75e3a

Browse files
committed
Merge pull request #739 from Urigo/bugfix/splicing-arrays
bugfix (get-updates): changed the way array changes are saved to mongo.
2 parents 196dff3 + fd068a6 commit ad75e3a

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

lib/get-updates.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
var getKeyPaths = function(obj) {
2525
var keys = _.keys(obj).map(function(k) {
2626
var v = obj[k];
27-
if (!_.isObject(v) || _.isEmpty(v)) return k;
27+
if (!_.isObject(v) || _.isEmpty(v) || _.isArray(v)) return k;
2828

2929
return getKeyPaths(v).map(function(subKey) {
3030
return k + '.' + subKey;
@@ -34,15 +34,17 @@
3434
return _.flatten(keys);
3535
};
3636

37-
var getDeepValues = function(obj) {
38-
var values = _.values(obj).map(function(v) {
39-
if (!_.isObject(v) || _.isEmpty(v))
40-
return v;
37+
var getDeepValues = function(obj,arr) {
38+
arr = arr || [];
39+
40+
_.values(obj).forEach(function(v) {
41+
if (!_.isObject(v) || _.isEmpty(v) || _.isArray(v))
42+
arr.push(v);
4143
else
42-
return getDeepValues(v);
44+
getDeepValues(v, arr);
4345
});
4446

45-
return flatten(values);
47+
return arr;
4648
};
4749

4850
var flatten = function(arr) {

modules/angular-meteor-object.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,19 @@ angularMeteorObject.factory('AngularMeteorObject', [
5656
}
5757
}
5858

59-
// NOTE: do not use #upsert() method, since it does not exist in some collections
60-
collection.update(this.$$id, mods, createFulfill({ action: 'updated' }));
59+
var pullUpdate;
60+
if (mods.$pull) {
61+
pullUpdate = { $pull : mods.$pull };
62+
}
63+
64+
if (!pullUpdate) {
65+
// NOTE: do not use #upsert() method, since it does not exist in some collections
66+
collection.update(this.$$id, mods, createFulfill({action: 'updated'}));
67+
}
68+
else {
69+
collection.update(this.$$id, mods);
70+
collection.update(this.$$id, pullUpdate, createFulfill({action: 'updated'}))
71+
}
6172
}
6273
// insert
6374
else {
@@ -70,7 +81,7 @@ angularMeteorObject.factory('AngularMeteorObject', [
7081
collection.insert(mods, createFulfill({ action: 'inserted' }));
7182
}
7283

73-
return deferred.promise;
84+
return deferred.promise;
7485
};
7586

7687
AngularMeteorObject.reset = function(keepClientProps) {
@@ -102,7 +113,7 @@ angularMeteorObject.factory('AngularMeteorObject', [
102113
delete self[prop];
103114
delete self._serverBackup[prop];
104115
});
105-
}
116+
}
106117

107118
else {
108119
_.keys(this.getRawObject()).forEach(function(prop) {
@@ -159,16 +170,28 @@ angularMeteorObject.factory('$meteorObject', [
159170
this.unregisterAutoBind = this._auto && $rootScope.$watch(function(){
160171
return self.getRawObject();
161172
}, function (item, oldItem) {
162-
if (item === oldItem) return;
173+
if (item === oldItem) {
174+
self.$$collection.update({_id: item._id}, self.getRawObject());
175+
return;
176+
}
163177

164178
var id = item._id;
165179
delete item._id;
166180
delete oldItem._id;
167181

168182
var updates = getUpdates(oldItem, item);
169183
if (_.isEmpty(updates)) return;
184+
var pullUpdate;
170185

186+
if (updates.$pull) {
187+
pullUpdate = { $pull : updates.$pull };
188+
delete updates.$pull;
189+
}
171190
self.$$collection.update({_id: id}, updates);
191+
192+
if (pullUpdate) {
193+
self.$$collection.update({ _id : id}, pullUpdate);
194+
}
172195
}, true);
173196

174197
this.unregisterAutoDestroy = $rootScope.$on('$destroy', function() {

tests/integration/angular-meteor-get-updates-spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('getUpdates module', function() {
1414
describe('validations', function() {
1515
it('should throw an error if first argument is not an object', function() {
1616
var boundGetUpdates = _.partial(getUpdates);
17-
expect(boundGetUpdates).toThrowError(/first argument.*object/);
17+
expect(boundGetUpdates).toThrowError(/first argument.*object/);
1818
});
1919

2020
it('should throw an error if second argument is not an object', function() {
@@ -343,4 +343,4 @@ describe('getUpdates module', function() {
343343
expect(actualUpdates).toDeepEqual(expectedUpdates);
344344
});
345345
});
346-
});
346+
});

tests/integration/angular-meteor-object-spec.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('$meteorObject service', function () {
2121

2222
beforeEach(function () {
2323
id = 'test_1';
24-
var data = { _id: id, a: 1, b: 2 };
24+
var data = { _id: id, a: 1, b: 2, d: [1,2,3] };
2525

2626
TestCollection = new Mongo.Collection(null);
2727
TestCollection.insert(data);
@@ -78,7 +78,8 @@ describe('$meteorObject service', function () {
7878
_id: id,
7979
a: 1,
8080
b: 2,
81-
c: 3
81+
c: 3,
82+
d: [1,2,3]
8283
});
8384

8485
Tracker.flush();
@@ -95,7 +96,8 @@ describe('$meteorObject service', function () {
9596
_id: id,
9697
a: 1,
9798
b: 2,
98-
c: 3
99+
c: 3,
100+
d: [1,2,3]
99101
});
100102
});
101103

@@ -133,7 +135,7 @@ describe('$meteorObject service', function () {
133135
meteorObject.clientProp = 'delete';
134136
meteorObject.reset(true);
135137

136-
expect(meteorObject.clientProp).toBeDefined('angular meteor object has client property');
138+
expect(meteorObject.clientProp).toBeDefined('angular meteor object has client property');
137139
});
138140

139141
it('should delete properties for an object that doesnt exist in the collection', function() {
@@ -182,5 +184,20 @@ describe('$meteorObject service', function () {
182184
expect(TestCollection.update.calls.mostRecent().args[0]).toEqual({ _id: id });
183185
expect(TestCollection.update.calls.mostRecent().args[1]).toEqual({ $unset: { 'c.d': true } });
184186
});
187+
188+
it('should save changes in a nested array', function() {
189+
meteorObject.d.splice(1,1);
190+
191+
$rootScope.$apply();
192+
193+
var doc = TestCollection.findOne(id);
194+
195+
expect(doc).toDeepEqual({
196+
_id : id,
197+
a : 1,
198+
b : 2,
199+
d : [1, 3]
200+
});
201+
});
185202
});
186203
});

0 commit comments

Comments
 (0)