Skip to content

Commit 288b65c

Browse files
committed
bugfix (get-updates): changed the way array changes are saved to mongo.
1 parent 7febfed commit 288b65c

File tree

3 files changed

+39
-62
lines changed

3 files changed

+39
-62
lines changed

lib/get-updates.js

Lines changed: 13 additions & 24 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) {
@@ -114,12 +116,13 @@
114116
if (_.isDate(srcValue) && _.isDate(dstValue)) {
115117
if (srcValue.getTime() != dstValue.getTime()) diff[k] = dstValue;
116118
}
117-
118-
if (_.isObject(srcValue) && _.isObject(dstValue)) {
119+
else if (_.isArray(srcValue) && _.isArray(dstValue) && !angular.equals(srcValue, dstValue)) {
120+
diff[k] = dstValue;
121+
}
122+
else if (_.isObject(srcValue) && _.isObject(dstValue)) {
119123
var valueDiff = getDifference(srcValue, dstValue);
120124
utils.setFilled(diff, k, valueDiff);
121125
}
122-
123126
else if (srcValue !== dstValue) {
124127
diff[k] = dstValue;
125128
}
@@ -141,12 +144,10 @@
141144

142145
var set = createSet(paths);
143146
var unset = createUnset(paths);
144-
var pull = createPull(unset);
145147

146148
var updates = {};
147149
utils.setFilled(updates, '$set', set);
148150
utils.setFilled(updates, '$unset', unset);
149-
utils.setFilled(updates, '$pull', pull);
150151

151152
return updates;
152153
};
@@ -166,18 +167,6 @@
166167
}, {});
167168
};
168169

169-
var createPull = function(unset) {
170-
var arrKeyPaths = _.keys(unset).map(function(k) {
171-
var split = k.match(/(.*)\.\d+$/);
172-
return split && split[1];
173-
});
174-
175-
return _.compact(arrKeyPaths).reduce(function(pull, k) {
176-
pull[k] = null;
177-
return pull;
178-
}, {});
179-
};
180-
181170
var getUndefinedKeys = function(obj) {
182171
return _.keys(obj).filter(function (k) {
183172
var v = obj[k];

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

Lines changed: 5 additions & 34 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() {
@@ -45,8 +45,7 @@ describe('getUpdates module', function() {
4545
var expectedUpdates = {
4646
$set: {
4747
'b.e': 'e',
48-
'd.2': 4,
49-
'd.3': 3
48+
'd' : [1,2,4,3]
5049
}
5150
};
5251

@@ -80,30 +79,6 @@ describe('getUpdates module', function() {
8079
expect(actualUpdates).toDeepEqual(expectedUpdates);
8180
});
8281

83-
it('should define a "$pull" property when the destination object has an array value with missing elements', function() {
84-
var src = {
85-
arr: [1, 2, 3, 4, 5]
86-
};
87-
88-
var dst = {
89-
arr: [1, 2, 3]
90-
};
91-
92-
var expectedUpdates = {
93-
$unset: {
94-
'arr.3': true,
95-
'arr.4': true
96-
},
97-
98-
$pull: {
99-
'arr': null
100-
}
101-
};
102-
103-
var actualUpdates = getUpdates(src, dst);
104-
expect(actualUpdates).toDeepEqual(expectedUpdates);
105-
});
106-
10782
it('should get updates when object-field is assigned a deep-object', function() {
10883
var src = {a: {}};
10984
var dst = {a: {L1: {L2: {L3: 'v'}}}};
@@ -183,9 +158,7 @@ describe('getUpdates module', function() {
183158

184159
expect(updates).toDeepEqual({
185160
$set: {
186-
'a.0': 1,
187-
'a.1': 2,
188-
'a.2': 3
161+
'a': [1,2,3],
189162
}
190163
});
191164
});
@@ -233,9 +206,7 @@ describe('getUpdates module', function() {
233206

234207
expect(updates).toDeepEqual({
235208
$set: {
236-
'a.subfield.0': 1,
237-
'a.subfield.1': 2,
238-
'a.subfield.2': 3
209+
'a.subfield': [1,2,3],
239210
}
240211
});
241212
});
@@ -343,4 +314,4 @@ describe('getUpdates module', function() {
343314
expect(actualUpdates).toDeepEqual(expectedUpdates);
344315
});
345316
});
346-
});
317+
});

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)