Retrieve raw data from backend

Hello,

I’m developing an app where I’m retrieving data from my REST api. All my endpoints send models. Except one, that can’t be retrieved as a model. It’s a raw array where the keys are dates and the values are an amount. This raw data is used to display a graph on the front.

Here is an aspect of the endpoint : entities/{id}/stats/

stats: [{
    id: 0,
    comments: [
        2017-02-02: 165,
        2017-02-03: 235,
        2017-02-04: 145,
        ...
    ],
    visits: [
        2017-02-02: 1040,
        2017-02-03: 2050,
        2017-02-04: 1102,
        ...
    ],
    ...
}]

Now the problem is that ember force me to use a model to retrieve it. I managed to put the top level in a model Stat, with attributes comments, visits, etc… The model is being enrich by ember but now I would like to get the raw data inside of these attributes and ember doesn’t allow to do so. It’s expecting a string, number, model etc… but not an array or javascript object. I cannot create a model because the attributes would be random dates.

How can you pass easily to a template such a structure ?

Is there a way to do so without using models ?

Thanks,

Matthieu the Lambert

You can add a raw transform.

transforms/raw.js

import DS from 'ember-data';

export default DS.Transform.extend({
  deserialize: function(serialized) {
    return serialized;
  },
  serialize: function(deserialized) {
    return deserialized;
  }
});

Then you can add this to your model.

stats: DS.attr('raw', {defaultValue: ()=>[]}),

You might want to consider not using Ember Data here, and just do an ajax request.

Hi @broerse and @Noone this is related to my question which is How to define the attributes on my model and how to make a query on my route.js - #2 by beddu can someone please help me too. The updated question was on the comment section.

hi @broerse i tried your approach but it’s still show an [object object] to me. Btw, this is my response

Below is the response from my API

"attributes": { "original-data": "{\"name\":\"Foobarz\",\"updated_at\":\"2018-02-27 08:06:14 UTC\"}", "new-data": "{\"name\":\"Foobar\",\"updated_at\":\"2018-02-27 10:55:17 UTC\"}", "event-name": "update", "ip-address": "121.58.000.106", "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36", "created-at": "2018-02-27T10:55:17.477Z", "updated-at": "2018-02-27T10:55:17.477Z" },

When i do {{model.eventName}} it show the event-name information which is update. However, when i did the {{model.newData}} it retrun a [object Object]. My question is, how can I display the new-data attribute filed value name and updated_at?

@mikeelemuel I only work with ember-pouch and have never seen your issue. Don’t think I can help with your Adapter. You must do something like this with the raw transform added to your transform folder if it is the raw data you want to see.

  newData:               DS.attr('raw'),
  originalData:          DS.attr('raw'),
1 Like

Why don’t just use DS.attr() ? Without type passed in it swallows data ‘as is’

1 Like

I know it works but because it is nowhere I know of speced it could change with a new ember-data version. The raw transform should always work.

Hey Matthieu, you might find this helpful: Non-standard REST Actions in Ember Data - Ember Igniter