Ember.js getters and setters

19 March 2014

To make all the magic fairy-dust sprinkling efficient, like auto-updating templates and computed properties, Ember uses getters and setters instead of accessing properties directly the javascript (and dare I say, the Angular) way. At its most simplest form, it is object.get(property) and object.set(property).

Howevers, it would not be Ember if we were not provided methods on top of these primitives to make our hard lives as web developers simpler. In the following post, I am going to show (some of) these methods through an example, so let's go.

Badges, badges, I want more badges

I am certainly in favor of having badges in every application. Discourse is actively discoursing their badge system so I quickly sketched out something to get ahead of them.

Let me just paste the code here and then use it to explain what the getter and setter methods are good for.

You can quickly scroll through the below templates. They are mostly there so that you get the whole picture. The stuff relevant to this discussion is found in the javascript code.

 1<script type="text/x-handlebars">
 2  <div class="container">
 3    <div class="panel panel-primary">
 4      <div class="panel-heading">
 5        <h3 class="panel-title">Your badges, sir/ma'am.</h3>
 6      </div>
 7      <div class="panel-body">
 8        {{outlet}}
 9      </div>
10    </div>
11  </div>
12 </script>
 1<script type="text/x-handlebars" data-template-name="index">
 2  <ul class="list-group">
 3    {{#each badge in arrangedContent}}
 4      <li {{bind-attr class=":list-group-item badge.unlocked::locked"}}>
 5        {{badge.name}}
 6        <span class="badge">{{badge.score}}</span>
 7      </li>
 8    {{/each}}
 9    <li class="list-group-item">
10      <em>Total:</em>
11      <span class="pull-right">{{totalScore}}</span>
12    </li>
13  </ul>
14  <div id="new-badge" class="row">
15    <span class="col-xs-6">
16      {{input class="input" type="text" placeholder="Badge name" value=name}}
17    </span>
18    <span class="col-xs-4">
19      {{input class="small-input" type="text" placeholder="Score" value=score action="addBadge"}}
20    </span>
21    <span class="col-xs-2">
22      <button class="btn btn-primary btn-sm pull-right" type="button"
23          {{action "addBadge"}}>
24          Add
25      </button>
26    </span>
27  </div>
28  <div id="unlock-all" class="row">
29    <div class="col-xs-12">
30      <button class="btn btn-danger btn-md" type="button"
31        {{action "unlockAll"}}>
32        Unlock all
33      </button>
34    </div>
35  </div>

First, the badge class is defined and some badges created so that we have something to work with:

 1App = Ember.Application.create();
 3App.Badge = Ember.Object.extend({
 4  name: '',
 5  score: 0,
 6  unlocked: false
 9var rook = App.Badge.create({
10  name: "R00k",
11  score: 1,
12  unlocked: true
15var talkative = App.Badge.create({
16  name: "Talkative",
17  score: 10
20var hemingway = App.Badge.create({
21  name: "Hemingway",
22  score: 1000
25App.Router.map(function() {
28App.IndexRoute = Ember.Route.extend({
29  model: function() {
30    return [rook, talkative, hemingway];
31  }

getProperties and setProperties

The first couple of methods come handy when working with a single object but multiple properties.

 1App.IndexController = Ember.ArrayController.extend({
 2  sortProperties: ['score'],
 3  sortAscending: true,
 5  (...)
 7  actions: {
 8    addBadge: function() {
 9      var newBadgeProperties = this.getProperties(['name', 'score']);
10      newBadgeProperties.score = parseInt(newBadgeProperties.score, 10);
12      var newBadge = App.Badge.create(newBadgeProperties);
13      this.get('model').pushObject(newBadge);
14      this.setProperties({ name: '', score: '' });
15    },
17    (...)
18  }

On line 9, we want to create a new object with the values provided in the input boxes (addBadge is the action that gets triggered when the Add button is clicked, check the template). getProperties will create a javascript object creating key-value pairs for the passed properties. So the above might e.g yield { name: "Silent Bob", score: "2" }. That gets directly passed in to create a new badge object.

On line 14, we use the mutating pair of getProperties to reset the input fields. Pretty straightforward.

getEach and setEach

Ember has us covered when we are working with an array and want to get (or set) the same property of each item.

 1App.IndexController = Ember.ArrayController.extend({
 2  sortProperties: ['score'],
 3  sortAscending: true,
 5  totalScore: function() {
 6    var sum = function(s1, s2) { return s1 + s2; };
 7    return this.get('model').getEach('score').reduce(sum);
 8  }.property('model.@each.score'),
10  actions: {
11    (...)
13    unlockAll: function() {
14      this.get('model').setEach('unlocked', true);
15    }
16  }

When the "Unlock all" button launches the unlockAll action, it calls setEach on the badges (line 14), making all of them unlocked (you can verify this in the demo by seeing the color of the badge names turn darker - their css class has been updated). Another advange of setEach is that it guards against calling set on null or undefined values.

You might know the reader counterpart, getEach as mapBy. It goes through the array and makes another array by getting the passed property on each item. So in the above example (line 7), we first collect the score for each badge and then sum them up by way of reduction. (A shiny example of a non-distributed map-reduce :) ).

A macro can make nice code nicer

I have used a reduce computed macro before to set up sorting. I have gotten the hang of it and realized I could use another couple of them to make the above code simpler (and more performant):

 1App.IndexController = Ember.ArrayController.extend({
 2  sortProperties: ['score'],
 3  sortAscending: true,
 5  scores: Ember.computed.mapBy('model', 'score'),
 6  totalScore: Ember.computed.sum('scores'),
 8  (...)

The problem is that the subject under discussion, getEach is gone now, so pretend you did not see this.

Finally, here is the jsbin, should you decide to play around with it:

Getter and Setters

I hope some of that sticks and you'll write less "bare" gets and sets.

Share on Twitter