Skip to content

Commit ee53739

Browse files
committed
work in progress
1 parent 73217b4 commit ee53739

6 files changed

+63
-8
lines changed

dist/js-data-jsonapi-light.js

+15-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data-jsonapi-light.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/adapter.ts

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ export class JsonApiAdapter extends HttpAdapter{
7171
// Ensure id is properly set
7272
props[mapper.idAttribute] = id;
7373

74+
if (!opts.replace) {
75+
opts.method = opts.method || 'patch';
76+
}
77+
78+
// We need the record to get changes
79+
let record = (<any>mapper).datastore.get(mapper.name, id);
80+
if (record) {
81+
console.info(record.changes());
82+
}
83+
7484
return this.handleBeforeLifecycle(opts).then(() => {
7585
return HttpAdapter.prototype.update.call(this, mapper, id, props, opts)
7686
}).then(this.handleResponse(opts))

src/serializer.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export function jsonApiSerialize (mapper:any, data:any, opts:any){
2020
// Just cache a pointer to relations for the Resource
2121
mapperCacheRelationByField(mapper);
2222

23+
if (opts.changes && id && mapper.changes(id)) {
24+
let changes = mapper.changes(id);
25+
console.info('changes', changes);
26+
}
27+
2328
let relationships:any = {}
2429

2530
for (let key in data) {
@@ -44,7 +49,9 @@ export function jsonApiSerialize (mapper:any, data:any, opts:any){
4449

4550
// Work for update or create, if an id is given, server should accept it
4651
if (id) output.data.id = id;
47-
if (Object.keys(relationships)) output.data.relationships = relationships;
52+
if (Object.keys(relationships).length) {
53+
output.data.relationships = relationships;
54+
}
4855

4956
return output;
5057
}

test/unit/crud/update.spec.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ describe('UPDATE', () => {
7979
reqPatch = reqPut = null;
8080
})
8181

82-
// @todo #9
83-
xit('the request should receive only modified fields per default', () => {
82+
it('the request should receive only modified fields per default', () => {
8483
return store.update('Article', ID, UPDATE_PARAMS).then((data) => {
8584
expect(reqPatch.body).to.deep.equal({
8685
data: {
@@ -102,7 +101,28 @@ describe('UPDATE', () => {
102101
})
103102
});
104103

105-
it('the request should receive all fields when used with replace: true', () => {
104+
it('the request should only received changes when a record is saved', () => {
105+
return store.find('Article', ID).then((record) => {
106+
record.title = UPDATE_PARAMS.title;
107+
return record.save();
108+
}).then((data) => {
109+
console.info(reqPatch.body);
110+
expect(reqPatch.body).to.deep.equal({
111+
data: {
112+
type: MAPPER_NAME,
113+
id: ID,
114+
attributes: { title: UPDATE_PARAMS.title }
115+
}
116+
})
117+
118+
expect(data).to.be.an('object')
119+
expect(data.id).to.equal(ID)
120+
expect(data.title).to.equal(UPDATE_PARAMS.title)
121+
expect(data.content).to.equal(FIND_RESPONSE.data.attributes.content)
122+
})
123+
});
124+
125+
it('the request should send relation when a record is saved.', () => {
106126
return store.update('Article', ID, UPDATE_PARAMS, {
107127
replace: true
108128
}).then((data) => {
@@ -123,6 +143,10 @@ describe('UPDATE', () => {
123143
expect(data.content).to.equal(FIND_RESPONSE.data.attributes.content)
124144
})
125145
});
146+
147+
it('the request should send all attributes and relationships when record is saved with option `replace`', () =>{
148+
return true;
149+
})
126150
})
127151

128152
describe('when use of unsupported methods', () => {

test/unit/lifecycle.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('Lifecycle', () => {
9595

9696
return store.update('User', USER.ID, {
9797
email: USER.EMAIL
98-
}, { beforeSerialize }).then(() => {
98+
}, { beforeSerialize, replace: true }).then(() => {
9999
expect(beforeSerialize.calledOnce).to.be.true;
100100
expect(req.body.data.attributes).to.deep.equal({
101101
email: USER.EMAIL,
@@ -122,7 +122,7 @@ describe('Lifecycle', () => {
122122

123123
return store.update('User', USER.ID, {
124124
email: USER.EMAIL
125-
}, { afterSerialize }).then(() => {
125+
}, { afterSerialize, replace: true }).then(() => {
126126
expect(afterSerialize.calledOnce).to.be.true;
127127
expect(req.body.data.attributes).to.deep.equal({
128128
email: USER.EMAIL,

0 commit comments

Comments
 (0)