Skip to content
This repository has been archived by the owner on May 26, 2019. It is now read-only.

ember-cli rewrite of the Models section #8

Merged
merged 1 commit into from
Mar 20, 2015
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
46 changes: 30 additions & 16 deletions source/models/connecting-to-an-http-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ data is structured.
The REST adapter uses the name of the model to determine what URL to
send JSON to.

For example, if you ask for an `App.Photo` record by ID:
For example, if you ask for a `photo` record by ID:

```js
App.PhotoRoute = Ember.Route.extend({
```app/routes/photo.js
export default Ember.Route.extend({
model: function(params) {
return this.store.find('photo', params.photo_id);
}
Expand Down Expand Up @@ -52,12 +52,18 @@ REST adapter:

Given the following models:

```js
App.Post = DS.Model.extend({
```app/models/post.js
import DS from 'ember-data';

export default DS.Model.extend({
title: DS.attr(),
comments: DS.hasMany('comment'),
user: DS.belongsTo('user')
});
```

```app/models/comment.js
import DS from 'ember-data';

App.Comment = DS.Model.extend({
body: DS.attr()
Expand Down Expand Up @@ -90,21 +96,29 @@ To quickly prototype a model and see the expected JSON, try using the [Ember Dat

### Customizing the Adapter

To customize the REST adapter, define a subclass of `DS.RESTAdapter` and
name it `App.ApplicationAdapter`. You can then override its properties
To customize the REST adapter, create a `app/adapters/application.js` file
and export a subclass of `DS.RESTAdapter`. You can then override its properties
and methods to customize how records are retrieved and saved.

```app/adapters/application.js
export default DS.RESTAdapter.extend({
...
});
```

#### Customizing a Specific Model

It's entirely possible that you need to define options for just one model instead of an application-wide customization. In that case, you can create an adapter named after the model you are specifying:

```js
App.PostAdapter = DS.RESTAdapter.extend({
```app/adapters/post.js
export default DS.RESTAdapter.extend({
namespace: 'api/v2',
host: 'https://api.example2.com'
});
```

App.PhotoAdapter = DS.RESTAdapter.extend({
```app/adapters/photo.js
export default DS.RESTAdapter.extend({
namespace: 'api/v1',
host: 'https://api.example.com'
});
Expand All @@ -124,8 +138,8 @@ particular person might go to `/api/v1/people/1`.

In that case, set `namespace` property to `api/v1`.

```js
App.ApplicationAdapter = DS.RESTAdapter.extend({
```app/adapters/application.js
export default DS.RESTAdapter.extend({
namespace: 'api/v1'
});
```
Expand All @@ -143,8 +157,8 @@ your server will need to be configured to send the correct CORS headers.

To change the host that requests are sent to, set the `host` property:

```js
App.ApplicationAdapter = DS.RESTAdapter.extend({
```app/adapters/application.js
export default DS.RESTAdapter.extend({
host: 'https://api.example.com'
});
```
Expand All @@ -159,8 +173,8 @@ property and Ember Data will send them along with each ajax request.

For Example

```js
App.ApplicationAdapter = DS.RESTAdapter.extend({
```app/adapters/application.js
export default DS.RESTAdapter.extend({
headers: {
'API_KEY': 'secret key',
'ANOTHER_HEADER': 'Some header value'
Expand Down
38 changes: 20 additions & 18 deletions source/models/customizing-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ more testable, easier to understand and reduces bloat for people who
may want to subclass your adapter.

If your backend has some consistent rules you can define an
`ApplicationAdapter`. The `ApplicationAdapter` will get priority over
`adapter:application`. The `adapter:application` will get priority over
the default Adapter, however it will still be superseded by model
specific Adapters.

```js
App.ApplicationAdapter = DS.RESTAdapter.extend({
```app/adapters/application.js
export default DS.RESTAdapter.extend({
// Application specific overrides go here
});
```

If you have one model that has exceptional rules for communicating
with its backend than the others you can create a Model specific
Adapter by naming an adapter "ModelName" + "Adapter".
Adapter by running the command `ember generate adapter adapter-name`".
For example, running `ember generate adapter post` will create the
following file:

```js
App.PostAdapter = DS.RESTAdapter.extend({
```app/adapters/post.js
export default DS.RESTAdapter.extend({
namespace: 'api/v1'
});
```
Expand Down Expand Up @@ -72,13 +74,13 @@ non-standard backends.
The `namespace` property can be used to prefix requests with a
specific url namespace.

```js
App.ApplicationAdapter = DS.RESTAdapter.extend({
```app/adapters/application.js
export default DS.RESTAdapter.extend({
namespace: 'api/1'
});
```

Requests for `App.Person` would now target `http://emberjs.com/api/1/people/1`.
Requests for `person` would now target `http://emberjs.com/api/1/people/1`.


#### Host Customization
Expand All @@ -87,13 +89,13 @@ By default the adapter will target the current domain. If you would
like to specify a new domain you can do so by setting the `host`
property on the adapter.

```js
App.ApplicationAdapter = DS.RESTAdapter.extend({
```app/adapters/application.js
export default DS.RESTAdapter.extend({
host: 'https://api.example.com'
});
```

Requests for `App.Person` would now target `https://api.example.com/people/1`.
Requests for `person` would now target `https://api.example.com/people/1`.


#### Path Customization
Expand All @@ -106,16 +108,16 @@ For example, if you did not want to pluralize model names and needed
underscore_case instead of camelCase you could override the
`pathForType` method like this:

```js
App.ApplicationAdapter = DS.RESTAdapter.extend({
```app/adapters/application.js
export default DS.RESTAdapter.extend({
pathForType: function(type) {
return Ember.String.underscore(type);
}
});
```

Requests for `App.Person` would now target `/person/1`.
Requests for `App.UserProfile` would now target `/user_profile/1`.
Requests for `person` would now target `/person/1`.
Requests for `userProfile` would now target `/user_profile/1`.

#### Authoring Adapters

Expand All @@ -129,8 +131,8 @@ adapter it is important to remember to set this property to ensure
Ember does the right thing in the case a user of your adapter
does not specify an `ApplicationSerializer`.

```js
MyCustomAdapterAdapter = DS.RESTAdapter.extend({
```app/adapters/my-custom-adapter.js
export default DS.RESTAdapter.extend({
defaultSerializer: '-default'
});
```
Expand Down
Loading