Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,23 @@ export default Ember.Route.extend({

If your errors aren't standard, the helper function for that error type can be used as the base to build your custom detection function.

## Usage with Ember Data

Ember AJAX provides a mixin that can be used in an Ember Data Adapter to avoid the networking code provided by Ember Data and rely on Ember AJAX instead. This serves as a first step toward true integration of Ember AJAX into Ember Data.

To use the mixin, you can include the mixin into an Adapter, like so:

```javascript
// app/adapters/application.js
import DS from 'ember-data';
import AjaxServiceSupport from 'ember-ajax/mixins/ajax-support';

export default DS.JSONAPIAdapter.extend(AjaxServiceSupport);
```

That's all the configuration required! If you want to customize the adapter, such as using an alternative AJAX service (like one you extended yourself), hooks to do so are provided; check out the mixin's implementation for details.

Note that instead of using the Ember Data error checking code in your application, you should use the ones provided by Ember AJAX.

## Testing

Expand Down
12 changes: 11 additions & 1 deletion addon/mixins/ajax-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ const {
isNone,
merge,
run,
runInDebug,
Test,
testing
testing,
warn
} = Ember;
const JSONAPIContentType = 'application/vnd.api+json';

Expand Down Expand Up @@ -121,6 +123,14 @@ export default Mixin.create({
};

hash.error = (jqXHR, textStatus, errorThrown) => {
runInDebug(function() {
let message = `The server returned an empty string for ${requestData.type} ${url}, which cannot be parsed into a valid JSON. Return either null or {}.`;
let validJSONString = !(textStatus === 'parsererror' && jqXHR.responseText === '');
warn(message, validJSONString, {
id: 'ds.adapter.returned-empty-string-as-JSON'
});
});

const payload = this.parseErrorResponse(jqXHR.responseText) || errorThrown;
let response;

Expand Down
46 changes: 46 additions & 0 deletions addon/mixins/ajax-support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Ember from 'ember';

const {
Mixin,
inject: { service },
computed: { alias }
} = Ember;

export default Mixin.create({

/**
* The AJAX service to send requests through
*
* @property {AjaxService} ajaxService
* @public
*/
ajaxService: service('ajax'),

/**
* @property {string} host
* @public
*/
host: alias('ajaxService.host'),

/**
* @property {string} namespace
* @public
*/
namespace: alias('ajaxService.namespace'),

/**
* @property {object} headers
* @public
*/
headers: alias('ajaxService.headers'),

ajax(url, type, options) {
options = this.ajaxOptions(...arguments);
return this.get('ajaxService').request(url, options);
},

ajaxOptions(url, type, options = {}) {
options.type = type;
return this.get('ajaxService').options(url, options);
}
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"ember-cli-release": "0.2.8",
"ember-cli-sri": "^2.1.0",
"ember-cli-uglify": "^1.2.0",
"ember-data": "^2.5.3",
"ember-disable-prototype-extensions": "^1.1.0",
"ember-disable-proxy-controllers": "^1.0.1",
"ember-export-application-global": "^1.0.4",
Expand Down
38 changes: 38 additions & 0 deletions tests/acceptance/ember-data-integration-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

import Pretender from 'pretender';
import { jsonResponse } from 'dummy/tests/helpers/json';

let server;
moduleForAcceptance('Acceptance | ember data integration', {
beforeEach() {
server = new Pretender();
},
afterEach() {
server.shutdown();
}
});

test('ember data adapter uses ember-ajax mixin', function(assert) {
assert.expect(2);

server.get('api/posts/1', function() {
assert.ok(true, 'Used the ember-ajax integration');
return jsonResponse(200, {
data: {
id: 1,
type: 'post',
attributes: {
title: 'Foo'
}
}
});
});

visit('/ember-data-test');

andThen(function() {
assert.equal(currentURL(), '/ember-data-test');
});
});
8 changes: 8 additions & 0 deletions tests/dummy/app/adapters/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import DS from 'ember-data';
import AjaxServiceSupport from 'ember-ajax/mixins/ajax-support';

const { JSONAPIAdapter } = DS;

export default JSONAPIAdapter.extend(AjaxServiceSupport, {
namespace: 'api'
});
6 changes: 6 additions & 0 deletions tests/dummy/app/models/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({
title: attr('string')
});
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Router = Ember.Router.extend({
});

Router.map(function() {
this.route('ember-data-test');
});

export default Router;
7 changes: 7 additions & 0 deletions tests/dummy/app/routes/ember-data-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Ember from 'ember';

export default Ember.Route.extend({
model() {
return this.store.find('post', 1);
}
});
1 change: 1 addition & 0 deletions tests/dummy/app/templates/ember-data-test.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{outlet}}