Note that there are some explanatory texts on larger screens.

plurals
  1. POEmber.js: Where does this method go?
    text
    copied!<p>Coming to Ember from Rails, one of the places I'm struggling is trying to figure out Ember's definitions of models, views, and controllers. </p> <p>I'm just testing out some sample Ember code. I'm getting my user events via the GitHub API, and I want to change the type name into something readable.</p> <p>I have a jsbin <a href="http://jsbin.com/UgOLujiF/1/" rel="nofollow">here</a>, but here's the gist:</p> <pre><code>App = Ember.Application.create(); App.IndexRoute = Ember.Route.extend({ model: function(){ return Ember.$.getJSON('https://api.github.com/users/thenickcox/events').then(function(data){ return data.splice(0,7); }); } }); </code></pre> <p>I have a method that types a type and returns a string:</p> <pre><code> interpretType: function(type){ if (type === 'PushEvent') { return 'Pushed'; } return name; } </code></pre> <p>In Rails, this would go on the model. But the only model here is the one that Ember created in memory by default (right?). So then I thought, it's something that each member of the array needs, because here's the view:</p> <pre><code>&lt;h3&gt; Some events&lt;/h3&gt; &lt;ul&gt; {{#each}} &lt;li&gt;I {{interpretType(type)}} to {{repo.name}}&lt;/li&gt; {{/each}} &lt;/ul&gt; </code></pre> <p>So is that something that goes on <code>Ember.ArrayController</code>? I tried that, like this:</p> <pre><code>App.IndexController = Ember.ArrayController.extend({ interpretType: function(type){ if (type === 'PushEvent') { return 'Pushed'; } return name; }.property() }); </code></pre> <p>That just gave me an error. Where do I put this?</p> <p>PS. So you don't have to look at the GitHub API, here's an example JSON object:</p> <pre><code>{ id: "1890853674", type: "CreateEvent", actor: { id: 702327, login: "thenickcox", gravatar_id: "63f35d9e50dfd73281126b051a51668a", url: "https://api.github.com/users/thenickcox", avatar_url: "https://2.gravatar.com/avatar/63f35d9e50dfd73281126b051a51668a?d=https%3A%2F%2Fa248.e.akamai.net%2Fassets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png&amp;r=x" }, repo: { id: 14463966, name: "thenickcox/whiskey_taster", url: "https://api.github.com/repos/thenickcox/whiskey_taster" }, payload: { ref: "master", ref_type: "branch", master_branch: "master", description: "My first ember/rails app" }, public: true, created_at: "2013-11-17T09:00:17Z" }, </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload