1

I'm new with emberjs so it may be possible that I totally missed a few things.

I am trying to render the body of a table with content I retrieve from an Ajax request.

I use an ArrayController and a {{#collection}} in my view that I bind to the content of the controller (as the doc suggests).

It works when I set an initial value to content inside my ArrayController but it doesn't automatically update when I call .set('content', [...]).

Note that it only fails when I set the tagName of my view to 'tobdy', it works if for example I use 'pre' (or pretty much anything else).

JSFiddles that show that issue:

My questions are:

  • is this a bug or did I not understand how it should be working?
  • any advice for rendering the body of a table in a template?

This is what my code looks like:

index.html

<table id="the-table">
  <thead>
    <tr>
    <th>status</th>
    <th>title</th>
    <th>url</th>
    <th># messages</th>
    <th>judging status</th>
  </tr>
</thead>
</table>

<script type="text/x-handlebars" data-template-name="submissions">
    {{#collection contentBinding="App.submissionsController"}}
      <tr>
        <td>{{content.status}}</td>
        <td>{{content.title}}</td>
        <td><a href="{{content.url}}">note</a></td>
        <td>{{content.nbMessages}}</td>
        <td>{{content.judgingStatus}}</td>
      </tr>
    {{/collection}}
</script>

index.js

var App = Em.Application.create({
  ready: function () {
    App.submissionsController.index();
    this._super();
  }
});

// model
App.Submission = Em.Object.extend({
  title: null,
  judgingStatus: null,
  status: null,
  url: null,
  nbMessages: 0
});

// controller... not really.
App.submissionsController = Em.ArrayController.create({
  content: [ App.Submission.create({title: 'kevin', status: 'a', judgingStatus: 'b', url: 'http://google.com', nbMessages: 1}) ],

  index: function () {
    // simulates data that arrives to the page after a few seconds (think ajax request)
    window.setTimeout(function () {
      App.submissionsController.set('content', [
          App.Submission.create({title: 'stefano', status: 'c', judgingStatus: 'd', url: 'http://stackoverflow.com', nbMessages: 2})
      ]);
    }, 2000);
  }
});

Em.View.create({
  templateName: 'submissions',
  tagName: 'tbody'
}).appendTo('#the-table');
midu
  • 1,589
  • 1
  • 9
  • 14

1 Answers1

0

Two things I did to fix the code which can be seen on jsfiddle.

  1. I would intermittently get uncaught exception: Error: <Ember.View:ember161> - Unable to find template "submissions" because the handlebar script is not evaluated before the app is inserted into the DOM. See here.

  2. Change the view to {{#each}} instead of the deprecated {{#collection}}. I haven't found a definite source on the deprecation – but it's indicated here and here.

Community
  • 1
  • 1
Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
  • Awesome! that works, thanks! I submitted a [pull request](https://github.com/emberjs/ember.js/pull/488) to update the documentation of `ArrayController`. – midu Feb 11 '12 at 22:52