1
votes

I'm learning Ember.js using a Ruby on Rails API server. I've got the routes, template, and model all setup and working - but the template is never re-rendered once the data has been loaded from the API server. I'm not getting any error messages, and I know the customer is being loaded from looking at the Ember inspector.

Customer list is supposed to be displayed after start customer not being displayed

Customer list is being loaded correctly from the API server: customer is being loaded from the API

Router

// javacripts/router.js
App.Router.map(function() {
  this.resource('customers', { path: "/" });
});

Customers Route

// javascripts/routes/customer_routes.js
App.CustomersRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('customer');
  },
  renderTemplate: function() {
    this.render('customers/index');
  }
});

Customer Model

// javascripts/models/customer.js
App.Customer = DS.Model.extend({
  name: DS.attr('string')
});

Customer Index Template

// javacripts/templates/customers/index.js.handlebars
<ul>
  <li>start</li>
  {{#each customers}}
    <li>{{name}}</li>
  {{/each}}
</ul>

Store

// javacripts/store.js
App.ApplicationAdapter = DS.ActiveModelAdapter.extend({
  namespace: 'api/v1'
});
2
Is anything actually loading, like your application template file?Ryan
@ryan - it looks like everything is being loaded just fine. Both application.js.handlebars & index.js.handlebars are being rendered. But the {{#each}} loop in index is always emptylightswitch05
I was wondering cause the extension your using is odd. usually files are just extended with .handlebars or .hbs I've never actually seen .js.handlebars.Ryan
@ryan I've been looking at discourse for examples since they use Ember & Rails. That is how they name their templates, so I did too. sourcelightswitch05

2 Answers

1
votes

Instead of

{{#each customers}}

It should read either

{{#each controller}}
  {{name}}
{{/each}}

or

{{#each customer in controller}}
    {{customer.name}}
{{/each}}

I have recently posted two screencasts. One showing how to get started with a new application, and one showing how to setup Grunt:

You might also get some use out of a talk I gave earlier this year as well, which goes through developing a simple application during the talk, including Ember Data.

1
votes

Your customers/index template is referencing a "customers" collection that doesn't exist.

Your route's model hook is returning an array of records, which makes Ember generate an Ember.ArrayController with its model set to your array of customers. It doesn't have a property called "customers", so the {{#each customers}} doesn't have anything to iterate over. If you change it to just {{#each}} (because this in this scope references the controller, which is array-like) or {{#each model}} (to explicitly access the model array of the ArrayController), it should work correctly.

Also, your renderTemplate hook in the Route is the default behavior, so you can just delete it.

Incidentally, I'd recommend just using an Ember JSBin or something while you're starting out and learning the basics, so when you need to ask for help, you can just link to the bin, and people have live code they can work with to help you out, with a minimum of effort. That low barrier to entry makes a big difference to people who are doing free work for internet points.