Skip to content

Commit

Permalink
feature: after hook and serializeAndPush feature
Browse files Browse the repository at this point in the history
  • Loading branch information
iezer committed Oct 28, 2018
1 parent c2b0889 commit a871c17
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
3 changes: 2 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import memberAction from './utils/member-action';
import collectionAction from './utils/collection-action';
import serializeAndPush from './utils/serialize-and-push';

export const classOp = collectionAction;
export const instanceOp = memberAction;

export { collectionAction, memberAction };
export { collectionAction, memberAction, serializeAndPush };
9 changes: 8 additions & 1 deletion addon/utils/collection-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export default function instanceOp(options) {
let adapter = this.store.adapterFor(modelName);
let fullUrl = buildOperationUrl(this, options.path, urlType, false);
let serializedPayload = (options.before && options.before.call(this, payload)) || payload;
return adapter.ajax(fullUrl, requestType, merge(options.ajaxOptions || {}, { data: serializedPayload }));
return adapter.ajax(fullUrl, requestType, merge(options.ajaxOptions || {}, { data: serializedPayload }))
.then(response => {
if (options.after && this.isDestroyed) {
return options.after.call(this, options, response);
}

return response;
});
};
}
9 changes: 8 additions & 1 deletion addon/utils/member-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export default function instanceOp(options) {
let adapter = this.store.adapterFor(modelName);
let fullUrl = buildOperationUrl(this, options.path, urlType);
let serializedPayload = (options.before && options.before.call(this, payload)) || payload;
return adapter.ajax(fullUrl, requestType, merge(options.ajaxOptions || {}, { data: serializedPayload }));
return adapter.ajax(fullUrl, requestType, merge(options.ajaxOptions || {}, { data: serializedPayload }))
.then(response => {
if (options.after && !this.isDestroyed) {
return options.after.call(this, options, response);
}

return response;
});
};
}
10 changes: 10 additions & 0 deletions addon/utils/serialize-and-push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { isArray } from '@ember/array';

export default function(options, response) {
let modelName = this.constructor.modelName || this.constructor.typeKey;
let modelClass = this.store.modelFor(modelName);
let serializer = this.store.serializerFor(modelName);
let normalized = isArray(response.data) ? serializer.normalizeArrayResponse(this.store, modelClass, response) :
serializer.normalizeSingleResponse(this.store, modelClass, response);
return this.store.push(normalized);
}
14 changes: 10 additions & 4 deletions tests/acceptance/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ module('Acceptance | index2', function(hooks) {
await click('.all-fruit .fresh-type-button');
});

test('before hook', async function(assert) {
test('before/after hooks and serializeAndPush helper', async function(assert) {
await visit('/');
assert.expect(4);
assert.expect(7);

server.put('/fruits/:id/doEat', request => {
let data = JSON.parse(request.requestBody);
Expand All @@ -76,7 +76,7 @@ module('Acceptance | index2', function(hooks) {
id: 1,
type: 'fruit',
attributes: {
name: 'apple'
name: 'Eaten apple'
}
}
};
Expand All @@ -90,7 +90,7 @@ module('Acceptance | index2', function(hooks) {
data: {
type: 'fruits',
attributes: {
name: 'apple',
name: 'Eaten apple',
was_eaten: true
}
}
Expand All @@ -111,8 +111,14 @@ module('Acceptance | index2', function(hooks) {
return [200, {}, JSON.stringify(response)];
});

assert.dom(`[data-test-fruit-name="apple"]`).exists();

await click('#apple .eat-instance-button');

assert.dom(`[data-test-fruit-name="Eaten apple"]`).exists();

await click('.all-fruit .eat-all-button');

assert.dom(`[data-test-fruit-name="Completely Eaten apple"]`).exists();
});
});
8 changes: 5 additions & 3 deletions tests/dummy/app/models/fruit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// BEGIN-SNIPPET fruit-model
import DS from 'ember-data';
import { memberAction, collectionAction } from 'ember-api-actions';
import { memberAction, collectionAction, serializeAndPush } from 'ember-api-actions';
import { assign } from '@ember/polyfills';

const { attr, Model } = DS;
Expand All @@ -19,10 +19,12 @@ export default Model.extend({
getFresh: collectionAction({ path: 'fresh', type: 'get' }),
eat: memberAction({
path: 'doEat',
before: mergeAttributes
before: mergeAttributes,
after: serializeAndPush
}),
eatAll: collectionAction({
path: 'doEatAll',
before: mergeAttributes })
before: mergeAttributes,
after: serializeAndPush })
});
// END-SNIPPET

0 comments on commit a871c17

Please sign in to comment.