8000 Closes #168 · StudyForFun/js-data@cbe7b07 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbe7b07

Browse files
committed
Closes js-data#168
1 parent bb94724 commit cbe7b07

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
##### 2.0.0-beta.11 - 26 June 2015
2+
3+
###### Backwards compatible API changes
4+
- #168 - DS#inject - replace instead of merge. `onConflict: 'replace'` will replace existing items instead of merging into them.
5+
16
##### 2.0.0-beta.10 - 26 June 2015
27

38
###### Backwards compatible bug fixes
@@ -53,7 +58,7 @@
5358

5459
###### Backwards compatible bug fixes
5560
- #127 - Memory leak in DS.changes
56-
- #134 - All resources get all methods defined on any resource
61+
- #134 - All resources get all methods defined on any resource
5762
- #142 - Allow omitting options in getEndpoint
5863

5964
##### 2.0.0-beta.4 - 28 April 2015

src/datastore/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ defaultsPrototype.maxAge = false;
121121
defaultsPrototype.meth 8000 ods = {};
122122
defaultsPrototype.notify = !!DSUtils.w;
123123
defaultsPrototype.omit = [];
124+
defaultsPrototype.onConflict= 'merge';
124125
defaultsPrototype.reapAction = !!DSUtils.w ? 'inject' : 'none';
125126
defaultsPrototype.reapInterval = !!DSUtils.w ? 30000 : false;
126127
defaultsPrototype.relationsEnumerable = false;

src/datastore/sync_methods/inject.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,22 @@ function _inject(definition, resource, attrs, options) {
226226
} else {
227227
// item is being re-injected
228228
// new properties take precedence
229-
DSUtils.deepMixIn(item, attrs);
229+
if (options.onConflict === 'merge') {
230+
DSUtils.deepMixIn(item, attrs);
231+
} else if (options.onConflict === 'replace') {
232+
DSUtils.forOwn(item, (v, k) => {
233+
if (k !== definition.idAttribute) {
234+
if (!attrs.hasOwnProperty(k)) {
235+
delete item[k];
236+
}
237+
}
238+
});
239+
DSUtils.forOwn(attrs, (v, k) => {
240+
if (k !== definition.idAttribute) {
241+
item[k] = v;
242+
}
243+
});
244+
}
230245

231246
if (definition.resetHistoryOnInject) {
232247
// clear change history for item

test/both/datastore/sync_methods/inject.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,22 @@ describe('DS#inject', function () {
312312
}
313313
assert.isTrue(foundParent, 'parent is enumerable');
314314
});
315+
it('should replace existing items', function () {
316+
var post = Post.inject(p1);
317+
post.foo = 'bar';
318+
post.beep = 'boop';
319+
assert.deepEqual(JSON.stringify(post), JSON.stringify({
320+
author: 'John',
321+
age: 30,
322+
id: 5,
323+
foo: 'bar',
324+
beep: 'boop'
325+
}));
326+
post = Post.inject(p1, { onConflict: 'replace' });
327+
assert.deepEqual(JSON.stringify(post), JSON.stringify({
328+
author: 'John',
329+
age: 30,
330+
id: 5
331+
}));
332+
});
315333
});

0 commit comments

Comments
 (0)
< 2957 /div>
0