Custom serializer for model loads, route has no data


#1

I’m building an employee directory for my company. I’m pulling data from a static JSON file so I’m using a custom Model Serializer to alter the data to add an ID (see this forum post).

The data is loading in via the overridden normalizePayload method within the PersonSerializer and the route loads without error, but no data is showing up on the page.

I’ve tried returning result directly from the method but that throws an error. I’ve tried walking the stack trace form my PeopleController’s init method but thats a rabbit hole. I wondered if anyone could suggest a way to debug this issue and help me identify where the data is being lost.

Here’s the relevant bits of my app:

App.Router.map(function () {
    this.resource('people', { path: '/people' }, function(){
        this.resource('person', { path: ':person_id'});
    });
});

App.PeopleRoute = Ember.Route.extend({
    name: 'people',
    model: function(){
        return this.store.find('person');
    }
});

App.PersonSerializer = DS.RESTSerializer.extend({
    normalizePayload: function(type, payload) {
        var result = [],
            obj;
        payload.forEach(function(el, index){
            el['id'] = index;
            result.push(el);
        });
        return {'person': result };
    }
});

App.Person = DS.Model.extend({
    First: DS.attr('string'),
    Last:  DS.attr('string')
});

App.PeopleController = Ember.ArrayController.extend({
    sortAscending: true,
    sortProperties:['First', 'Last'],
    init: function(){
        debugger;
    }
});

#2

Alright, so I was able to get this code to work by manually adding data to the content array within my PeopleController like so:

App.PeopleController = Ember.ArrayController.extend({
    sortAscending: true,
    sortProperties:['First', 'Last'],
    init: function(){
        this.set('people', this.store.all('person').get('content'));
        this.set('content', this.store.all('person').get('content'));
    }
});

But I don’t feel I should have to do this. Can anyone offer insight as to why this needs to be done? Is it a pluralization issue between Person and People?


#3

Ahhh. I’ve got it. My serializer was returning this:

return {'person': result };

When it should have been returning this:

return {'persons': result };

Problem solved.