Skip to content

Commit

Permalink
replaces this.get pattern with ES5 getter where possible (#2303)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspanring committed Jun 10, 2018
1 parent f001d70 commit b28bf1d
Show file tree
Hide file tree
Showing 33 changed files with 151 additions and 157 deletions.
8 changes: 4 additions & 4 deletions guides/v3.2.0/applications/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ import Route from '@ember/routing/route';
export default Route.extend({
activate() {
// The logger property is injected into all routes
this.get('logger').log('Entered the index route!');
this.logger.log('Entered the index route!');
}
});
```
Expand Down Expand Up @@ -245,13 +245,13 @@ import { getOwner } from '@ember/application';
export default Component.extend({
audioService: computed('song.audioType', function() {
let applicationInstance = getOwner(this);
let audioType = this.get('song.audioType');
let audioType = this.song.audioType;
return applicationInstance.lookup(`service:audio-${audioType}`);
}),

click() {
let player = this.get('audioService');
player.play(this.get('song.file'));
let player = this.audioService;
player.play(this.song.file);
}
});
```
2 changes: 1 addition & 1 deletion guides/v3.2.0/applications/run-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ let User = EmberObject.extend({
lastName: null,

fullName: computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
return `${this.firstName} ${this.lastName}`;
})
});
```
Expand Down
10 changes: 4 additions & 6 deletions guides/v3.2.0/applications/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export default Service.extend({
},

add(item) {
this.get('items').pushObject(item);
this.items.pushObject(item);
},

remove(item) {
this.get('items').removeObject(item);
this.items.removeObject(item);
},

empty() {
this.get('items').clear();
this.items.clear();
}
});
```
Expand Down Expand Up @@ -108,12 +108,10 @@ export default Component.extend({
```

Injected properties are lazy loaded; meaning the service will not be instantiated until the property is explicitly called.
Therefore you need to access services in your component using the `get` function otherwise you might get an undefined.

Once loaded, a service will persist until the application exits.

Below we add a remove action to the `cart-contents` component.
Notice that below we access the `cart` service with a call to `this.get`.

```javascript {data-filename=app/components/cart-contents.js}
import Component from '@ember/component';
Expand All @@ -124,7 +122,7 @@ export default Component.extend({

actions: {
remove(item) {
this.get('cart').remove(item);
this.cart.remove(item);
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions guides/v3.2.0/components/defining-a-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import Route from '@ember/routing/route';

export default Route.extend({
model() {
return this.get('store').findAll('post');
return this.store.findAll('post');
}
});
```
Expand Down Expand Up @@ -103,7 +103,7 @@ import Route from '@ember/routing/route';

export default Route.extend({
model() {
return this.get('store').findAll('post');
return this.store.findAll('post');
}
});
```
Expand Down
2 changes: 1 addition & 1 deletion guides/v3.2.0/components/handling-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default Component.extend({

drop(event) {
let id = event.dataTransfer.getData('text/data');
this.get('action')(id);
this.action(id);
}
});
```
Expand Down
6 changes: 3 additions & 3 deletions guides/v3.2.0/components/passing-properties-to-a-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Route from '@ember/routing/route';

export default Route.extend({
model() {
return this.get('store').findAll('post');
return this.store.findAll('post');
}
});
```
Expand Down Expand Up @@ -93,10 +93,10 @@ import { computed } from '@ember/object';

export default Component.extend({
title: computed('params.[]', function(){
return this.get('params')[0];
return this.params[0];
}),
body: computed('params.[]', function(){
return this.get('params')[1];
return this.params[1];
})
}).reopenClass({
positionalParams: 'params'
Expand Down
10 changes: 5 additions & 5 deletions guides/v3.2.0/components/the-component-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import Component from '@ember/component';
export default Component.extend({
init() {
this._super(...arguments);
this.errors = [];
this.set('errors', []);
},

didUpdateAttrs() {
Expand All @@ -77,7 +77,7 @@ export default Component.extend({
actions: {
required(event) {
if (!event.target.value) {
this.get('errors').pushObject({ message: `${event.target.name} is required`});
this.errors.pushObject({ message: `${event.target.name} is required`});
}
}
}
Expand All @@ -101,7 +101,7 @@ import Component from '@ember/component';
export default Component.extend({
didReceiveAttrs() {
this._super(...arguments);
const profile = this.get('data');
const profile = this.data;
if (typeof profile === 'string') {
this.set('profile', JSON.parse(profile));
} else {
Expand Down Expand Up @@ -237,8 +237,8 @@ export default Component.extend({

didReceiveAttrs() {
this._super(...arguments);
this.set('items', this.get('items').map((item) => {
if (item.id === this.get('selectedItem.id')) {
this.set('items', this.items.map((item) => {
if (item.id === this.selectedItem.id) {
item.isSelected = true;
}
return item;
Expand Down
18 changes: 9 additions & 9 deletions guides/v3.2.0/components/triggering-changes-with-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default Component.extend({

actions: {
userDidDeleteAccount() {
this.get('login').deleteUser();
this.login.deleteUser();
}
}
});
Expand Down Expand Up @@ -176,7 +176,7 @@ export default Component.extend({

submitConfirm() {
//call the onConfirm property to invoke the passed in action
this.get('onConfirm')();
this.onConfirm();
},

cancelConfirm() {
Expand All @@ -186,7 +186,7 @@ export default Component.extend({
});
```

`this.get('onConfirm')` will return the function passed from the parent as the
`this.onConfirm` will return the function passed from the parent as the
value of `onConfirm`, and the following `()` will invoke the function.

Like normal attributes, actions can be a property on the component; the
Expand Down Expand Up @@ -222,7 +222,7 @@ export default Component.extend({

submitConfirm() {
// call onConfirm with the value of the input field as an argument
let promise = this.get('onConfirm')();
let promise = this.onConfirm();
promise.then(() => {
this.set('confirmShown', false);
});
Expand Down Expand Up @@ -271,7 +271,7 @@ Within `button-with-confirmation`, the code in the `submitConfirm` action does n
It will still invoke `onConfirm` without explicit arguments:

```javascript {data-filename=app/components/button-with-confirmation.js}
const promise = this.get('onConfirm')();
const promise = this.onConfirm();
```
However the expression `(action "sendMessage" "info")` used in passing the action to the component creates a closure,
i.e. an object that binds the parameter we've provided to the function specified.
Expand Down Expand Up @@ -315,7 +315,7 @@ export default Component.extend({
//...
submitConfirm() {
// call onConfirm with a second argument
let promise = this.get('onConfirm')(this.get('confirmValue'));
let promise = this.onConfirm(this.confirmValue);
promise.then(() => {
this.set('confirmShown', false);
});
Expand Down Expand Up @@ -417,8 +417,8 @@ export default Component.extend({

actions: {
userDidDeleteAccount() {
this.get('login').deleteUser();
this.get('didDelete')(this.get('login.currentUserObj'));
this.login.deleteUser();
this.didDelete(this.login.currentUserObj);
}
}
});
Expand Down Expand Up @@ -463,7 +463,7 @@ export default Component.extend({
login: service(),
actions: {
deleteUser(idStr) {
return this.get('login').deleteUserAccount(idStr);
return this.login.deleteUserAccount(idStr);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ import { computed } from '@ember/object';

// This won't work:
fullName: function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
return `${this.firstName} ${this.lastName}`;
}.property('firstName', 'lastName')


// Instead, do this:
fullName: computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
return `${this.firstName} ${this.lastName}`;
})
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ store.createRecord('post', {
});
```

The store object is available in controllers and routes using `this.get('store')`.
The store object is available in controllers and routes using `this.store`.

## Updating Records

Making changes to Ember Data records is as simple as setting the attribute you
want to change:

```javascript
this.get('store').findRecord('person', 1).then(function(tyrion) {
this.store.findRecord('person', 1).then(function(tyrion) {
// ...after the record has loaded
tyrion.set('firstName', 'Yollo');
});
Expand Down
2 changes: 1 addition & 1 deletion guides/v3.2.0/models/customizing-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default DS.JSONAPIAdapter.extend({
session: service('session'),
headers: computed('session.authToken', function() {
return {
'API_KEY': this.get('session.authToken'),
'API_KEY': this.session.authToken,
'ANOTHER_HEADER': 'Some header value'
};
})
Expand Down
2 changes: 1 addition & 1 deletion guides/v3.2.0/models/defining-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default DS.Model.extend({
lastName: DS.attr(),

fullName: computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
return `${this.firstName} ${this.lastName}`;
})
});
```
Expand Down
10 changes: 5 additions & 5 deletions guides/v3.2.0/models/finding-records.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ Use [`store.findRecord()`](https://www.emberjs.com/api/ember-data/release/classe
This will return a promise that fulfills with the requested record:

```javascript
let blogPost = this.get('store').findRecord('blog-post', 1); // => GET /blog-posts/1
let blogPost = this.store.findRecord('blog-post', 1); // => GET /blog-posts/1
```

Use [`store.peekRecord()`](https://www.emberjs.com/api/ember-data/release/classes/DS.Store/methods/findRecord?anchor=peekRecord) to retrieve a record by its type and ID, without making a network request.
This will return the record only if it is already present in the store:

```javascript
let blogPost = this.get('store').peekRecord('blog-post', 1); // => no network request
let blogPost = this.store.peekRecord('blog-post', 1); // => no network request
```

### Retrieving Multiple Records

Use [`store.findAll()`](https://www.emberjs.com/api/ember-data/release/classes/DS.Store/methods/findAll?anchor=findAll) to retrieve all of the records for a given type:

```javascript
let blogPosts = this.get('store').findAll('blog-post'); // => GET /blog-posts
let blogPosts = this.store.findAll('blog-post'); // => GET /blog-posts
```

Use [`store.peekAll()`](http://emberjs.com/api/data/classes/DS.Store.html#method_peekAll) to retrieve all of the records for a given type that are already loaded into the store, without making a network request:

```javascript
let blogPosts = this.get('store').peekAll('blog-post'); // => no network request
let blogPosts = this.store.peekAll('blog-post'); // => no network request
```

`store.findAll()` returns a `DS.PromiseArray` that fulfills to a `DS.RecordArray` and `store.peekAll` directly returns a `DS.RecordArray`.
Expand All @@ -47,7 +47,7 @@ For example, we could search for all `person` models who have the name of

```javascript
// GET to /persons?filter[name]=Peter
this.get('store').query('person', {
this.store.query('person', {
filter: {
name: 'Peter'
}
Expand Down
2 changes: 1 addition & 1 deletion guides/v3.2.0/models/handling-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Along with the records returned from your store, you'll likely need to handle so
Pagination is a common example of using metadata. Imagine a blog with far more posts than you can display at once. You might query it like so:

```javascript
let result = this.get('store').query('post', {
let result = this.store.query('post', {
limit: 10,
offset: 0
});
Expand Down
2 changes: 1 addition & 1 deletion guides/v3.2.0/models/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ have a `Person` model. An individual record in your app might
have a type of `person` and an ID of `1` or `steve-buscemi`.

```javascript
this.get('store').findRecord('person', 1); // => { id: 1, name: 'steve-buscemi' }
this.store.findRecord('person', 1); // => { id: 1, name: 'steve-buscemi' }
```

An ID is usually assigned to a record by the server when you save it for
Expand Down
6 changes: 3 additions & 3 deletions guides/v3.2.0/models/pushing-records-into-the-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Route from '@ember/routing/route';

export default Route.extend({
model() {
this.get('store').push({
this.store.push({
data: [{
id: 1,
type: 'album',
Expand Down Expand Up @@ -97,7 +97,7 @@ import Route from '@ember/routing/route';

export default Route.extend({
model() {
this.get('store').pushPayload({
this.store.pushPayload({
albums: [
{
id: 1,
Expand Down Expand Up @@ -138,7 +138,7 @@ export default Route.extend({
method: 'POST',
url: 'process-payment'
}).then((digitalInventory) => {
this.get('store').push(digitalInventory);
this.store.push(digitalInventory);
this.transitionTo('thank-you');
});
}
Expand Down
Loading

0 comments on commit b28bf1d

Please sign in to comment.