Skip to content

Commit

Permalink
Avoid Ember array extension methods (mdn#35673)
Browse files Browse the repository at this point in the history
* ember prototype fix

* update text

* review comments

* Update index.md

* Update files/en-us/learn/tools_and_testing/client-side_javascript_frameworks/ember_interactivity_events_state/index.md

* Update files/en-us/learn/tools_and_testing/client-side_javascript_frameworks/ember_interactivity_events_state/index.md

* Update files/en-us/learn/tools_and_testing/client-side_javascript_frameworks/ember_conditional_footer/index.md

* Update files/en-us/learn/tools_and_testing/client-side_javascript_frameworks/ember_interactivity_events_state/index.md

---------

Co-authored-by: Joshua Chen <[email protected]>
  • Loading branch information
gagan-bhullar-tech and Josh-Cena authored Sep 3, 2024
1 parent e011d56 commit cfb8ef1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ To get the footer working, we need to implement the following three areas of fun
```js
get incomplete() {
return this.todos.filterBy('isCompleted', false);
return this.todos.filter((todo) => !todo.isCompleted);
}
```
Using Ember's [`ArrayProxy.filterBy()`](https://api.emberjs.com/ember/4.2/classes/ArrayProxy/methods/filterBy?anchor=filterBy) method, we're able to easily filter Objects in our array based on simple equals conditions. Here we're asking for all the todo items where the `isCompleted` property is equal to `false`, and because `isCompleted` is `@tracked` in our `Todo` object, this getter will re-compute when the value changes on an Object in the array.
Using the [`filter()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) method, we're asking for all the todo items where the `isCompleted` property is equal to `false`, and because `isCompleted` is `@tracked` in our `Todo` object, this getter will re-compute when the value changes on an Object in the array.
4. Next, add the following action underneath the existing `add(text)` action:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,14 @@ export default class TodoDataService extends Service {
add(text) {
let newTodo = new Todo(text);

this.todos.pushObject(newTodo);
this.todos = [...this.todos, newTodo];
}
}
```

Here, the `todos` property on the service will maintain our list of todos contained inside an array, and we'll mark it with `@tracked`, because when the value of `todos` is updated we want the UI to update as well.

And just like before, the `add()` function that will be called from the template gets annotated with the `@action` decorator to bind it to the class instance.
While this may look like familiar JavaScript, you may notice that we're calling the method [`pushObject()`](https://api.emberjs.com/ember/4.2/classes/ArrayProxy/methods/filterBy?anchor=pushObject) on our `todos` array.
This is because Ember extends JavaScript's Array prototype by default, giving us convenient methods to ensure Ember's tracking system knows about these changes.
There are dozens of these methods, including `pushObjects()`, `insertAt()`, or `popObject()`, which can be used with any type (not just Objects).
Ember's [ArrayProxy](https://api.emberjs.com/ember/4.2/classes/ArrayProxy) also gives us more handy methods, like `isAny()`, `findBy()`, and `filterBy()` to make life easier.
And just like before, the `add()` function that will be called from the template gets annotated with the `@action` decorator to bind it to the class instance. We [spread our `todos` array](/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) to add `newTodo` to it, which creates a new array and will trigger the reactivity system to update the UI.

## Using the service from our header component

Expand Down

0 comments on commit cfb8ef1

Please sign in to comment.