Skip to content

Commit

Permalink
Fixes #316
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Sep 22, 2016
1 parent d5e6628 commit fd16d2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/datastore/sync_methods/inject.js
Expand Up @@ -251,7 +251,13 @@ function _inject (definition, resource, attrs, options) {
// new properties take precedence
if (options.onConflict === 'merge') {
DSUtils.deepMixIn(item, attrs)
DSUtils.forOwn(definition.computed, function (fn, field) {
DSUtils.compute.call(item, fn, field)
})
} else if (options.onConflict === 'replace') {
DSUtils.forOwn(definition.computed, function (fn, field) {
DSUtils.compute.call(attrs, fn, field)
})
DSUtils.forOwn(item, function (v, k) {
if (k !== definition.idAttribute) {
if (!attrs.hasOwnProperty(k)) {
Expand Down
1 change: 0 additions & 1 deletion test/browser/datastore/async_methods/refreshAll.test.js
Expand Up @@ -14,7 +14,6 @@ describe('DS#refreshAll', function () {
return Post.refreshAll({
age: 33
}).then(function (posts) {
console.log(posts);
assert.equal(posts.length, 2);
assert.equal(Post.getAll().length, 2);
assert.isDefined(Post.get(8));
Expand Down
21 changes: 19 additions & 2 deletions test/browser/datastore/sync_methods/defineResource.test.js
Expand Up @@ -55,7 +55,7 @@ describe('DS#defineResource', function () {
computed: {
fullName: function (first, last) {
callCount++;
return first + ' ' + last;
return (first || '') + (last ? ' ' + last : '');
}
}
});
Expand Down Expand Up @@ -135,6 +135,24 @@ describe('DS#defineResource', function () {

assert.equal(callCount, 5, 'fullName() should have been called 3 times');

person = store.inject('person', { id: 1, first: 'Bill', email: 'bill.anderson@test.com' });

assert.objectsEqual(person, {
first: 'Bill',
last: 'Anderson',
email: 'bill.anderson@test.com',
id: 1,
fullName: 'Bill Anderson'
});

person = store.inject('person', { id: 1, first: 'William' }, { onConflict: 'replace' });

assert.objectsEqual(person, {
first: 'William',
id: 1,
fullName: 'William'
});

done();
}, 50);
}, 50);
Expand Down Expand Up @@ -408,7 +426,6 @@ describe('DS#defineResource', function () {

setTimeout(function () {
assert.equal(4, _this.requests.length);
console.log(_this.requests[3]);
assert.equal(_this.requests[3].url, 'blah/1/test2');
assert.equal(_this.requests[3].method, 'GET');
_this.requests[3].respond(200, {'Content-Type': 'text/plain'}, 'bleh');
Expand Down

0 comments on commit fd16d2a

Please sign in to comment.