Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an "extract" option property to "pass" related response key to promise resolve. #203

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 22 additions & 9 deletions addon/utils/collection-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ import { buildOperationUrl } from './build-url';
const { merge } = Ember;

export default function instanceOp(options) {
return function(payload) {
let modelName = this.constructor.modelName || this.constructor.typeKey;
let requestType = (options.type || 'PUT').toUpperCase();
let urlType = options.urlType || requestType;
let adapter = this.store.adapterFor(modelName);
let fullUrl = buildOperationUrl(this, options.path, urlType, false);
return adapter.ajax(fullUrl, requestType, merge(options.ajaxOptions || {}, { data: payload }));
};
}
return function(payload) {
let modelName = this.constructor.modelName || this.constructor.typeKey;
let requestType = (options.type || 'PUT').toUpperCase();
let urlType = options.urlType || requestType;
let adapter = this.store.adapterFor(modelName);
let fullUrl = buildOperationUrl(this, options.path, urlType, false);
let extract = options.extract;
return new Ember.RSVP.Promise(function(resolve, reject) {
return adapter.ajax(fullUrl, requestType, merge(options.ajaxOptions || {}, { data: payload })).then(function(response) {
if (extract) {
if (response && response[extract] !== null) {
return resolve(response[extract]);
} else {
return reject(new Error('Response malformed'));
}
} else {
return resolve(response);
}
}).catch(reject);
});
};
}
31 changes: 22 additions & 9 deletions addon/utils/member-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ import { buildOperationUrl } from './build-url';
const { merge } = Ember;

export default function instanceOp(options) {
return function(payload) {
let modelName = this.constructor.modelName || this.constructor.typeKey;
let requestType = (options.type || 'PUT').toUpperCase();
let urlType = options.urlType || requestType;
let adapter = this.store.adapterFor(modelName);
let fullUrl = buildOperationUrl(this, options.path, urlType);
return adapter.ajax(fullUrl, requestType, merge(options.ajaxOptions || {}, { data: payload }));
};
}
return function(payload) {
let modelName = this.constructor.modelName || this.constructor.typeKey;
let requestType = (options.type || 'PUT').toUpperCase();
let urlType = options.urlType || requestType;
let adapter = this.store.adapterFor(modelName);
let fullUrl = buildOperationUrl(this, options.path, urlType);
let extract = options.extract;
return new Ember.RSVP.Promise(function(resolve, reject) {
return adapter.ajax(fullUrl, requestType, merge(options.ajaxOptions || {}, { data: payload })).then(function(response) {
if (extract) {
if (response && response[extract] !== null) {
return resolve(response[extract]);
} else {
return reject(new Error('Response malformed'));
}
} else {
return resolve(response);
}
}).catch(reject);
});
};
}
1 change: 1 addition & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]}