Skip to content

Commit 4850bc4

Browse files
committed
transformJsonapiAttrs
1 parent 78219ea commit 4850bc4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

addon/mixins/nested-relations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const attributesFor = function(record) {
4848
}
4949
});
5050

51-
return attrs;
51+
return record.transformJsonapiAttrs ? record.transformJsonapiAttrs(attrs) : attrs;
5252
};
5353

5454
const jsonapiPayload = function(record, isManyToManyDelete) {

tests/unit/mixins/nested-relations-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,31 @@ test('it serializes basic attributes correctly', function(assert) {
152152
assert.deepEqual(json, expectedJSON, 'has correct json');
153153
});
154154

155+
test('it uses transformJsonapiAttrs to modify attributes', function(assert) {
156+
let post = store.createRecord('post', { title: 'test post' });
157+
158+
post.transformJsonapiAttrs = function(attrs) {
159+
return {
160+
titleUpcase: attrs.title.toUpperCase(),
161+
publishedDate: this.get('publishedDate')
162+
};
163+
};
164+
165+
let json = serialize(post, {});
166+
167+
let expectedJSON = {
168+
data: {
169+
type: 'posts',
170+
attributes: {
171+
titleUpcase: 'test post'.toUpperCase(),
172+
publishedDate: post.get('publishedDate')
173+
}
174+
}
175+
};
176+
177+
assert.deepEqual(json, expectedJSON, 'has correct json');
178+
});
179+
155180
test('it respects custom keyForAttribute settings in serializer', function(assert) {
156181
let date = new Date();
157182
let post = store.createRecord('post', { publishedDate: date });

0 commit comments

Comments
 (0)