1
votes

I'm fairly new to Ember and I'm starting to write more complex apps. I've been pulling hair trying to figure out why defining a controller for the index breaks the index route's model population.

I've tried the "setupcontroller" function, but still no luck.

Here's the route code:

App.IndexRoute = Ember.Route.extend({
    model: function () {
        return Ember.RSVP.hash({ //return promises for both models here
            featuredJobs: $.getJSON('http://api.*********/featured/jobs', {'token': guestToken}),
            featuredEmployers: $.getJSON('http://api.********/featured/employers', {'token': guestToken})
        })
    }
});

When I add App.IndexController = Ember.Controller.extend({... it breaks the model's {{#each}} helper. (not the app). I can see the model assigned to the route in the Ember inspector. Here's the template:

<div class="panel-body">
    <div class="list-group ft-jobs">
        {{#each featuredJobs}}
            {{#linkTo 'job' _id class="list-group-item"}}
            <h4 class="list-group-item-heading">{{title}}</h4>
            <p class="list-group-item-text">{{description}}</p>
        {{/linkTo}}

        {{else}}
        <p class="text-center">Sorry, no featured jobs are available.</p>
        {{/each}} //END OF SNIPPET
1
I may be wrong, but I think the IndexController needs to be an Ember.ArrayController instead of just Ember.Controller, because it's expected an array of JSON objects.rogMaHall
That would make sense... Thanks, I'll try it tomorrow.James_1x0
Report back on how it goes if you can :) I'm fairly new to Ember as well, so that was just my first instinct if everything else was working before adding the controller.rogMaHall
@rogMaHall Well, my node api feeds back a json object, so kingpin2k's answer was correct, but you had the right idea. It works now.James_1x0
Awesome! Yeah, that makes sense, I upvoted him right away when I saw his answer. I was thinking of it in terms of multiple json objects being handled by a single controller :ProgMaHall

1 Answers

3
votes

Your controller needs to extend ObjectController since it's being backed by an object.

http://emberjs.jsbin.com/OxIDiVU/134/edit