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

Replaces `this.get with ES5 getter where possible (#2303) in v3.2.0 #87

Merged
merged 1 commit into from
Jul 29, 2018
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
11 changes: 7 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 @@ -244,14 +244,17 @@ import { getOwner } from '@ember/application';
//
export default Component.extend({
audioService: computed('song.audioType', function() {
if (!this.song) {
return null;
}
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
4 changes: 2 additions & 2 deletions guides/v3.2.0/components/handling-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ various draggable behaviors. For example, a component may need to send an `id`
when it receives a drop event:

```handlebars
{{drop-target action=(action "didDrop")}}
{{drop-target dropAction=(action "didDrop")}}
```

You can define the component's event handlers to manage the drop event.
Expand All @@ -69,7 +69,7 @@ export default Component.extend({

drop(event) {
let id = event.dataTransfer.getData('text/data');
this.get('action')(id);
this.dropAction(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
13 changes: 6 additions & 7 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', []);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really good catch, thank you

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seconded 🎉

},

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,12 +237,11 @@ export default Component.extend({

didReceiveAttrs() {
this._super(...arguments);
this.set('items', this.get('items').map((item) => {
if (item.id === this.get('selectedItem.id')) {
this.items.forEach((item) => {
if (item.id === this.selectedItem.id) {
item.isSelected = true;
}
return item;
}));
});
},

didRender() {
Expand Down
19 changes: 8 additions & 11 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,9 +186,6 @@ export default Component.extend({
});
```

`this.get('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
only difference is that the property is set to a function that knows how
to trigger behavior.
Expand Down Expand Up @@ -222,7 +219,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 +268,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 +312,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 +414,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 +460,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
20 changes: 5 additions & 15 deletions guides/v3.2.0/models/finding-records.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +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
// GET /blog-posts/1
this.get('store').findRecord('blog-post', 1)
.then(function(blogPost) {
// Do something with `blogPost`
});
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
// no network request
let blogPost = this.get('store').peekRecord('blog-post', 1);
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
// GET /blog-posts
this.get('store').findAll('blog-post')
.then(function(blogPosts) {
// Do something with `blogPosts`
});
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
// no network request
let blogPosts = this.get('store').peekAll('blog-post');
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 @@ -57,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
Loading