Skip to content

Commit

Permalink
Merge pull request emberjs#1086 from twokul/ed-guides
Browse files Browse the repository at this point in the history
final clean ups for ED
  • Loading branch information
rwjblue committed Nov 21, 2013
2 parents 3b41eec + 7a9d7f1 commit 234f886
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 107 deletions.
8 changes: 4 additions & 4 deletions data/guides.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ Controllers:
Models:
- title: "Introduction"
url: "models"
- title: "Using the Store"
url: "models/using-the-store"
- title: "Defining Models"
url: "models/defining-models"
- title: "Finding Records"
url: "models/finding-records"
- title: "Creating and Deleting Records"
url: "models/creating-and-deleting-records"
- title: "Pushing Records into the Store"
url: "models/pushing-records-into-the-store"
- title: "Persisting Records"
url: "models/persisting-records"
- title: "Finding Records"
url: "models/finding-records"
#- title: "Filtering Records"
#url: "models/filtering-records"
- title: "Working with Records"
Expand Down
19 changes: 8 additions & 11 deletions source/guides/models/creating-and-deleting-records.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ The store object is available in controllers and routes using `this.store`.
Although `createRecord` is fairly straightforward, the only thing to watch out for
is that you cannot assign a promise as a relationship, currently.

For example, if you wanted to set the author of a post, this would **not** work
if the `user` with id wasn't already loaded into the store:
For example, if you want to set the `author` property of a post, this would **not** work
if the `user` with id isn't already loaded into the store:

```js
var store = this.store;
Expand Down Expand Up @@ -42,26 +42,23 @@ store.find('user', 1).then(function(user) {

### Deleting Records

Deleting records is just as straightforward as creating records. Just call `.deleteRecord()`
Deleting records is just as straightforward as creating records. Just call `deleteRecord()`
on any instace of `DS.Model`. This flags the record as `isDeleted` and thus removes
it from `.all()` queries on the `store`. The deletion can then be persisted using `model.save()`.
it from `all()` queries on the `store`. The deletion can then be persisted using `save()`.
Alternatively, you can use the `destroyRecord` method to delete and persist at the same time.

```js
var post = store.find('post', 1);

post.deleteRecord();

post.get('isDeleted');
// => true
post.get('isDeleted'); // => true

post.save();
// => DELETE to /posts/1
post.save(); // => DELETE to /posts/1

// OR

var post = store.find('post', 2);

post.destroyRecord();
// => DELETE to /posts/1
```
post.destroyRecord(); // => DELETE to /posts/1
```
17 changes: 8 additions & 9 deletions source/guides/models/finding-records.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
`store.find()` allows you to find all records, single records, and query for records.
The first argument to `.find` is always the type of record in question. E.g. `post`. The second
argument is optional and can either be a plain object of search options or an id. Below are some examples:
The first argument to `store.find()` is always the type of record in question, e.g. `post`. The second
argument is optional and can either be a plain object of search options or an id. Below are some examples:

### Finding All Records of a Type


```js
var posts = store.find('post');
var posts = this.store.find('post');
```

This will return an instance of `DS.RecordArray`. As with records, the
record array will start in a loading state with a `length` of `0`.
When the server responds with results, any references to the record array
will update automatically.

**Note** `DS.RecordArray` is not a JavaScript array, it is an object that
**Note**: `DS.RecordArray` is not a JavaScript array, it is an object that
implements `Ember.Enumerable`. If you want to, for example, retrieve
records by index, you must use the `objectAt(index)` method. Since the
object is not a JavaScript array, using the `[]` notation will not work.
For more information, see [Ember.Enumerable][1] and [Ember.Array][2].

To get a list of records already loaded into the store, without firing
another network request, use `store.all('post')` instead.

[1]: http://emberjs.com/api/classes/Ember.Enumerable.html
[2]: http://emberjs.com/api/classes/Ember.Array.html

**Another Note** To get a list of records already loaded into the store, without firing
another network request, use `store.all('post')` instead.

### Finding a Single Record

You can retrieve a record by passing its model and unique ID to the `find()`
Expand Down Expand Up @@ -60,7 +59,7 @@ App.Router.map(function() {

App.PostRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('post', params.post_id);
return this.store.find('post', params.post_id);
}
});
```
Expand Down
43 changes: 19 additions & 24 deletions source/guides/models/persisting-records.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
Records in Ember Data are persisted on a per-instance
basis. Call `model.save()` on any instance of `DS.Model` and it will make a network request.
Here are a few examples:
Records in Ember Data are persisted on a per-instance basis.
Call `save()` on any instance of `DS.Model` and it will make a network request.

Here are a few examples:

```javascript
var post = store.createRecord('post', {
title: 'Rails is Omakase',
body: 'Lorem ipsum'
});

post.save()

// => POST to '/posts'
post.save(); // => POST to '/posts'
```

```javascript
var post = store.find('post', 1)

post.get('title')
// => "Rails is Omakase"
var post = store.find('post', 1);

post.set('title', 'A new post')
post.get('title') // => "Rails is Omakase"

post.save()
post.set('title', 'A new post');

// => PUT to '/posts/1'
post.save(); // => PUT to '/posts/1'
```

### Promises

Every `.save()` return a promise, so it is extremely easy to deal with success/failure
of the network request. Here's a common pattern:
`save()` returns a promise, so it is extremely easy to handle success and failure scenarios.
Here's a common pattern:

```javascript
var post = store.createRecord('post', {
Expand Down Expand Up @@ -71,24 +67,23 @@ You can read more about promises [here](https://github.com/tildeio/rsvp.js), but
example showing how to retry persisting:

```javascript
function retry(promise, retryCallback, nTimes) {
// if the promise fails,

return promise.fail(function(reason) {
function retry(callback, nTimes) {
// if the promise fails
return callback().fail(function(reason) {
// if we haven't hit the retry limit
if (nTimes-- > 0) {
// retry again with the result of calling the retry callback
// and the new retry limit
return retry(retryCallback(), retryCallback, nTimes);
return retry(callback, nTimes);
}

// otherwise, if we hit the retry limit, rethrow the error
throw reason;
});
}

// try to save the post up to 5 times
retry(post.save(), function() {
retry(function() {
return post.save();
}, 5);
```
```
53 changes: 0 additions & 53 deletions source/guides/models/using-the-store.md

This file was deleted.

9 changes: 3 additions & 6 deletions source/guides/models/working-with-records.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ modifying attributes. For example, you can use `Ember.Object`'s
`incrementProperty` helper:

```js
person.incrementProperty('age');
// Happy birthday!
person.incrementProperty('age'); // Happy birthday!
```

You can tell if a record has outstanding changes that have not yet been
saved by checking its `isDirty` property.

```js
person.get('isDirty');
//=> false
person.get('isDirty'); //=> false

person.set('isAdmin', true);

person.get('isDirty');
//=> true
person.get('isDirty'); //=> true
```

0 comments on commit 234f886

Please sign in to comment.