diff --git a/addon/utils/collection-action.js b/addon/utils/collection-action.js index 6912e679..4c817d98 100644 --- a/addon/utils/collection-action.js +++ b/addon/utils/collection-action.js @@ -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); + }); + }; +} \ No newline at end of file diff --git a/addon/utils/member-action.js b/addon/utils/member-action.js index 05ca171b..940e06d4 100644 --- a/addon/utils/member-action.js +++ b/addon/utils/member-action.js @@ -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); + }); + }; +} \ No newline at end of file diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 00000000..f408cac8 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1 @@ +{"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]} \ No newline at end of file