New to ember, can't even get simple JSON from mirage!


#1

My mirage route handler

  export default function () {
        this.get('/records', function (db, request) {
            return {
                "data": {
                    "records": [
                        { "date": "24-Apr-07", "close": 93.24 },
                        { "date": "25-Apr-07", "close": 95.35 },
                        { "date": "26-Apr-07", "close": 98.84 },
                        { "date": "27-Apr-07", "close": 99.92 },
                        { "date": "30-Apr-07", "close": 99.8 },
                        { "date": "1-May-07", "close": 99.47 },
                        { "date": "2-May-07", "close": 100.39 },
                        { "date": "3-May-07", "close": 100.4 },
                        { "date": "4-May-07", "close": 100.81 },
                        { "date": "7-May-07", "close": 103.92 },
                        { "date": "8-May-07", "close": 105.06 },
                        { "date": "9-May-07", "close": 106.88 }
                    ]
                }
            }
        });
}

My model

import DS from 'ember-data';

export default DS.Model.extend({});

My route

export default Ember.Route.extend({
    model() {
        return this.store.findAll('record');
    }
});

And voila my error filled chrome console log!


#2

Hi @rssfrncs, that is invalid JSON API (the array should be in data, there’s no id or type, etc). You can refer to http://jsonapi.org/format/ for details.

data: [
  { id: 1, type: "record", attributes: { "date": "24-Apr-07", "close": 93.24 } },
  ...
] 

Also, you haven’t defined any attributes on your model?


#3

hi thanks, i have followed you advice but what is being returned is not the json data instead it is an object?


#4

Have you updated your model with attributes?


#5

hi yes i have. i think it is my lack of understanding of promises…


#6

Okay i managed to get data returning not sure if this is correct, pointers would be great guys.

export default Ember.Route.extend({
    model() {
        return this.store.findAll('line').then(function(data){
          return data.toArray()
        })
    }
});

Then in my component i map using the get accessor on each item,

          d3data = data.map(function(d){
              d.x = formatDate.parse(d.get('x'));
              d.y = +d.get('y');
              return d;
          })

#7

That looks good! I think returning this.store.findAll('line') would be enough. You should be able to map over the array Ember Data’s promise returns.