mercredi 27 mai 2015

Ember JS - Unable to loop through an array returned from the model even though it is an array?

I have two Ember JS models (in an Ember CLI application):

  • list
  • link

Each list can have multiple links and as a result, I've declared my list model as:

import DS from 'ember-data';

export default DS.Model.extend({
    title       : DS.attr('string'),
    slug        : DS.attr('string'),
    visibility  : DS.attr('string'),
    owner       : DS.attr('string'),
    links       : DS.hasMany('link')
});

This is what the link model looks like:

import DS from 'ember-data';

export default DS.Model.extend({
    title   : DS.attr('string'),
    shortUrl: DS.attr('string'),
    views   : DS.attr('number'),
    owner   : DS.attr('string')
});

Within the list.js route, I make a call to fetch the list and its links like this:

model: function(params) {
        // Get list properties and links
        var list = this.store.find('list', params.listName);

        return list;
    },

The REST adapter correctly makes the call and my server returns the following response:

{
  "lists": [
    {
      "title": "Design",
      "slug": "design",
      "visibility": "private",
      "owner": "5540b2fb9611f67a07f7f6c1",
      "id": "5565ae05ca217589bc2a1bdf",
      "links": [
        1,
        2,
        3
      ]
    }
  ],
  "links": [
    {
      "id": 1,
      "title": "Dribbble - Show and tell for designers",
      "shortUrl": "http://sl.fi/a1CRgc",
      "views": 144,
      "owner": "5540b2fb9611f67a07f7f6c1"
    },
    {
      "id": 2,
      "title": "Dribbble - Show and tell for designers",
      "shortUrl": "http://sl.fi/a1CRgc",
      "views": 144,
      "owner": "5540b2fb9611f67a07f7f6c1"
    },
    {
      "id": 3,
      "title": "Dribbble - Show and tell for designers",
      "shortUrl": "http://sl.fi/a1CRgc",
      "views": 144,
      "owner": "5540b2fb9611f67a07f7f6c1"
    }
  ]
}

I modeled my response based on the Ember Data Model Maker. I have a list template that should loop through the links in the model so I am doing this:

{{#each links in model}}
    <span>{{ links.title }}</span>
{{/each}}

I get the following error when I load my application and I just can't seem to figure out a solution:

Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed <web-app@model:list::ember388:5565ae05ca217589bc2a1bdf>

Can someone please help me with a solution?




Aucun commentaire:

Enregistrer un commentaire