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

[DOC adapter] convert examples to native/Octane class syntax #7286

Merged
merged 2 commits into from
Sep 9, 2020
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
54 changes: 27 additions & 27 deletions packages/adapter/addon/-private/build-url-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import { pluralize } from 'ember-inflector';
```javascript
import Adapter, { BuildURLMixin } from '@ember-data/adapter';

export default Adapter.extend(BuildURLMixin, {
findRecord: function(store, type, id, snapshot) {
export default class ApplicationAdapter extends Adapter.extend(BuildURLMixin) {
findRecord(store, type, id, snapshot) {
var url = this.buildURL(type.modelName, id, snapshot, 'findRecord');
return this.ajax(url, 'GET');
}
});
}
```

### Attributes
Expand Down Expand Up @@ -125,12 +125,12 @@ export default Mixin.create({
```app/adapters/user.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default JSONAPIAdapter.extend({
export default class ApplicationAdapter extends JSONAPIAdapter {
urlForFindRecord(id, modelName, snapshot) {
let baseUrl = this.buildURL(modelName, id, snapshot);
return `${baseUrl}/users/${snapshot.adapterOptions.user_id}/playlists/${id}`;
}
});
}
```

@method urlForFindRecord
Expand All @@ -152,12 +152,12 @@ export default Mixin.create({
```app/adapters/comment.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default JSONAPIAdapter.extend({
export default class ApplicationAdapter extends JSONAPIAdapter {
urlForFindAll(modelName, snapshot) {
let baseUrl = this.buildURL(modelName);
return `${baseUrl}/data/comments.json`;
}
});
}
```

@method urlForFindAll
Expand All @@ -177,8 +177,8 @@ export default Mixin.create({
```app/adapters/application.js
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
host: 'https://api.github.com',
export default class ApplicationAdapter extends RESTAdapter {
host = 'https://api.github.com';
urlForQuery (query, modelName) {
switch(modelName) {
case 'repo':
Expand All @@ -187,7 +187,7 @@ export default Mixin.create({
return this._super(...arguments);
}
}
});
}
```

@method urlForQuery
Expand All @@ -207,12 +207,12 @@ export default Mixin.create({
```app/adapters/application.js
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
export default class ApplicationAdapter extends RESTAdapter {
urlForQueryRecord({ slug }, modelName) {
let baseUrl = this.buildURL();
return `${baseUrl}/${encodeURIComponent(slug)}`;
}
});
}
```

@method urlForQueryRecord
Expand All @@ -234,12 +234,12 @@ export default Mixin.create({
```app/adapters/application.js
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
export default class ApplicationAdapter extends RESTAdapter {
urlForFindMany(ids, modelName) {
let baseUrl = this.buildURL();
return `${baseUrl}/coalesce`;
}
});
}
```

@method urlForFindMany
Expand All @@ -261,12 +261,12 @@ export default Mixin.create({
```app/adapters/application.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default JSONAPIAdapter.extend({
export default class ApplicationAdapter extends JSONAPIAdapter {
urlForFindHasMany(id, modelName, snapshot) {
let baseUrl = this.buildURL(modelName, id);
return `${baseUrl}/relationships`;
}
});
}
```

@method urlForFindHasMany
Expand All @@ -288,12 +288,12 @@ export default Mixin.create({
```app/adapters/application.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default JSONAPIAdapter.extend({
export default class ApplicationAdapter extends JSONAPIAdapter {
urlForFindBelongsTo(id, modelName, snapshot) {
let baseUrl = this.buildURL(modelName, id);
return `${baseUrl}/relationships`;
}
});
}
```

@method urlForFindBelongsTo
Expand All @@ -315,11 +315,11 @@ export default Mixin.create({
```app/adapters/application.js
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
export default class ApplicationAdapter extends RESTAdapter {
urlForCreateRecord(modelName, snapshot) {
return this._super(...arguments) + '/new';
}
});
}
```

@method urlForCreateRecord
Expand All @@ -339,11 +339,11 @@ export default Mixin.create({
```app/adapters/application.js
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
export default class ApplicationAdapter extends RESTAdapter {
urlForUpdateRecord(id, modelName, snapshot) {
return `/${id}/feed?access_token=${snapshot.adapterOptions.token}`;
}
});
}
```

@method urlForUpdateRecord
Expand All @@ -364,11 +364,11 @@ export default Mixin.create({
```app/adapters/application.js
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
export default class ApplicationAdapter extends RESTAdapter {
urlForDeleteRecord(id, modelName, snapshot) {
return this._super(...arguments) + '/destroy';
}
});
}
```

@method urlForDeleteRecord
Expand Down Expand Up @@ -438,12 +438,12 @@ export default Mixin.create({
import { decamelize } from '@ember/string';
import { pluralize } from 'ember-inflector';

export default RESTAdapter.extend({
pathForType: function(modelName) {
export default class ApplicationAdapter extends RESTAdapter {
pathForType(modelName) {
var decamelized = decamelize(modelName);
return pluralize(decamelized);
}
});
}
```

@method pathForType
Expand Down
20 changes: 10 additions & 10 deletions packages/adapter/addon/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ import EmberError from '@ember/error';
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import MaintenanceError from './maintenance-error';

export default JSONAPIAdapter.extend({
export default class ApplicationAdapter extends JSONAPIAdapter {
handleResponse(status) {
if (503 === status) {
return new MaintenanceError();
}

return this._super(...arguments);
}
});
}
```

And can then be detected in an application and used to send the user to an
Expand All @@ -55,7 +55,7 @@ import EmberError from '@ember/error';
import Route from '@ember/routing/route';
import MaintenanceError from '../adapters/maintenance-error';

export default Route.extend({
export default class ApplicationRoute extends Route {
actions: {
error(error, transition) {
if (error instanceof MaintenanceError) {
Expand All @@ -66,7 +66,7 @@ import EmberError from '@ember/error';
// ...other error handling logic
}
}
});
}
```

@class AdapterError
Expand Down Expand Up @@ -136,10 +136,10 @@ AdapterError.extend = extendFn(AdapterError);
```app/models/post.js
import Model, { attr } from '@ember-data/model';

export default Model.extend({
title: attr('string'),
content: attr('string')
});
export default class PostModel extends Model {
@attr('string') title;
@attr('string') content;
}
```

To show an error from the server related to the `title` and
Expand All @@ -151,7 +151,7 @@ AdapterError.extend = extendFn(AdapterError);
import RESTAdapter from '@ember-data/adapter/rest';
import { InvalidError } from '@ember-data/adapter/error';

export default RESTAdapter.extend({
export default class ApplicationAdapter extends RESTAdapter {
updateRecord() {
// Fictional adapter that always rejects
return RSVP.reject(new InvalidError([
Expand All @@ -165,7 +165,7 @@ AdapterError.extend = extendFn(AdapterError);
}
]));
}
});
}
```

Your backend may use different property names for your records the
Expand Down
Loading