Tracked and Computed Property
Learn to use tracked and computed properties in Ember controllers.
We'll cover the following...
Tracked property
Tracked properties are used as a decorator to mark a property as tracked. They’re imported from @glimmer/tracking
. When a property is marked as tracked, it rerenders the DOM whenever the value of that tracked property is changed. A counter with the increment or decrement functionality needs to be tracked. This allows us to change it without having to render the DOM. Let’s create a counter layout in the practice
template to learn about the tracked property:
{{page-title "Practice"}}<br><h3 >{{this.count}}</h3><br><button class= "btn btn-success" {{on 'click' this.increment}}>+1 </button><button class= "btn btn-danger " {{on 'click' this.decrement}}>-1</button>{{!-- {{outlet}} --}}
-
Line 3: We add a
count
property in anh3
tag. We usethis
to refer to the properties from controllers. -
Line 5: We create a button and add an
increment
event handler. -
Line 6: We create a button and add a
decrement
event handler.
Now, let’s ...