diff --git a/README.md b/README.md index 9d4ac6b2..ed520fa6 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,18 @@ myRecord.ripen({ ``` +## Customization + +ember-api-actions generates URLs and ajax configuration via ember-data adapters. It will identify the appropriate adapter, and call the `buildURL` and `ajaxOptions` methods to send a JSON request similar to way conventional ember-data usage works. + +Customizing your adapter should customize requests sent out via this library, along with any other ember-data requests. + +**ember-api-actions uses the following methods on DS.Adapter** + +* [buildURL](http://emberjs.com/api/data/classes/DS.RESTAdapter.html#method_buildURL) - for generating an action's URL +* [ajaxOptions](https://github.com/emberjs/data/blob/v1.13.4/packages/ember-data/lib/adapters/rest-adapter.js#L928-L950) (private) - so that consumers may customize ajax options (i.e., `xhrFields`) in a single place +* [ajax](https://github.com/emberjs/data/blob/v1.13.4/packages/ember-data/lib/adapters/rest-adapter.js#L836-L859) (private) - to actually make the API request and return a promise + ## Installation * `git clone` this repository diff --git a/addon/index.js b/addon/index.js index a3ecbbb4..34f1b4e2 100644 --- a/addon/index.js +++ b/addon/index.js @@ -1,5 +1,4 @@ import Ember from 'ember'; -import ajax from 'ic-ajax'; const buildOperationUrl = function buildOperationUrl(record, opPath, requestType, intance=true) { Ember.assert('You must provide a path for instanceOp', opPath); @@ -13,28 +12,22 @@ const buildOperationUrl = function buildOperationUrl(record, opPath, requestType export const instanceOp = function instanceOp(options) { return function(payload) { let requestType = options.type || 'PUT'; + let modelName = this.constructor.modelName; + let adapter = this.store.adapterFor(modelName); let fullUrl = buildOperationUrl(this, options.path, requestType); - return ajax(Ember.$.extend(options.ajaxOptions || {}, { - type: requestType, - url: fullUrl, - contentType: 'application/json', - dataType: 'json', - data: JSON.stringify(payload) - })); + let ajaxOptions = adapter.ajaxOptions(fullUrl, requestType, { data: payload }); + return adapter.ajax(fullUrl, requestType, Ember.$.extend(options.ajaxOptions || {}, ajaxOptions)); }; }; export const classOp = function instanceOp(options) { return function(payload) { let requestType = options.type || 'PUT'; + let modelName = this.constructor.modelName; + let adapter = this.store.adapterFor(modelName); let fullUrl = buildOperationUrl(this, options.path, requestType, false); - return ajax(Ember.$.extend(options.ajaxOptions || {}, { - type: requestType, - url: fullUrl, - contentType: 'application/json', - dataType: 'json', - data: JSON.stringify(payload) - })); + let ajaxOptions = adapter.ajaxOptions(fullUrl, requestType, { data: payload }); + return adapter.ajax(fullUrl, requestType, Ember.$.extend(options.ajaxOptions || {}, ajaxOptions)); }; }; diff --git a/package.json b/package.json index ec6b534e..00f0c3be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-api-actions", - "version": "0.0.1", + "version": "0.0.2", "description": "Trigger remote actions that don't fit into CRUD, in your Ember.js apps", "directories": { "doc": "doc", diff --git a/tests/dummy/app/controllers/index.js b/tests/dummy/app/controllers/index.js index 42162a91..187970d3 100644 --- a/tests/dummy/app/controllers/index.js +++ b/tests/dummy/app/controllers/index.js @@ -5,6 +5,7 @@ export default Ember.Controller.extend({ this._super(...arguments); this.set('requests', Ember.A()); }, + // BEGIN-SNIPPET controller actions: { ripenFruit(fruit) { fruit.ripen({ @@ -17,4 +18,5 @@ export default Ember.Controller.extend({ }); } } + // END-SNIPPET }); diff --git a/tests/dummy/app/styles/app.css b/tests/dummy/app/styles/app.css index ec4e45d8..21adee03 100644 --- a/tests/dummy/app/styles/app.css +++ b/tests/dummy/app/styles/app.css @@ -1,3 +1,7 @@ .payload { margin-left: 30px; } + +.m-t-40 { + margin-top: 40px; +} diff --git a/tests/dummy/app/templates/application.hbs b/tests/dummy/app/templates/application.hbs index e7509121..cbe51745 100644 --- a/tests/dummy/app/templates/application.hbs +++ b/tests/dummy/app/templates/application.hbs @@ -1,5 +1,13 @@
-

Ember api actions

+
+
+

Ember api actions

+
+ +
+ {{outlet}}
diff --git a/tests/dummy/app/templates/index.hbs b/tests/dummy/app/templates/index.hbs index f7d36fb8..b019de38 100644 --- a/tests/dummy/app/templates/index.hbs +++ b/tests/dummy/app/templates/index.hbs @@ -1,23 +1,33 @@ -

API actions on an individual resource

-{{#each content as |fruit|}} -

-{{fruit.id}} {{fruit.name}} - - calls fruit.ripen() -

- -{{/each}} -

API action on a collection of resources

-

- {{content.constructor.modelName}} - - calls fruit.ripenAll() -

- - -{{code-snippet name='fruit-model.js'}} - -
Outgoing API Requests:
- \ No newline at end of file +
+
+

API actions on an individual resource

+ {{#each content as |fruit|}} +

+ + {{fruit.name}} (id = {{fruit.id}}) + - calls fruit.ripen() +

+ {{/each}} +

API action on a collection of resources

+

+ {{content.constructor.modelName}} + - calls fruit.ripenAll() +

+
+
+

Outgoing API Requests:

+ +
+
+
+
+ app/models/fruit.js + {{code-snippet name='fruit-model.js'}} + app/controllers/index.js + {{code-snippet name='controller.js'}} +
+
\ No newline at end of file diff --git a/tests/dummy/config/environment.js b/tests/dummy/config/environment.js index eca98787..b72cebe1 100644 --- a/tests/dummy/config/environment.js +++ b/tests/dummy/config/environment.js @@ -12,6 +12,13 @@ module.exports = function(environment) { // e.g. 'with-controller': true } }, + contentSecurityPolicy: { + 'img-src': '*', + 'connect-src': '*', + 'font-src': '*', + 'script-src': '\'unsafe-inline\' *', + 'style-src': '\'unsafe-inline\' *' + }, APP: { // Here you can pass flags/options to your application instance @@ -39,10 +46,10 @@ module.exports = function(environment) { ENV.APP.rootElement = '#ember-testing'; } - if (environment === 'development') { - ENV.locationType = 'hash'; - ENV.baseURL = 'ember-api-actions/'; - } + // if (environment === 'development') { + // ENV.locationType = 'hash'; + // ENV.baseURL = 'ember-api-actions/'; + // } return ENV; };