Lot of time to understand ember data!


#1

I’m looking for a backend in PHP to serve my EMBER-DATA , my choise is https://github.com/alixaxel/ArrestDB , but it 's not 100% compatible ember data. so , i’m working to adapte it .

The question is : the URL sending by ember data is :

GET

  api/posts  
  api/posts/1 
  api/posts/1/comments
  api/posts/1/comments/2,3

how can i give the good json response format , because if you have belongsTo , the json response was differente to hasMany …


#2

I am not sure of the question you’re asking, can you reword that?

json-api has some pretty thorough documentation around formatting responses http://jsonapi.org/format/.


#3

you are right, my english is not good, so my question i not clear.

ember-data <======> PHP REST

i have :
POST > hasMany >COMMENTS

app.js :

  ...
 this.get("store").find("posts") ;  
  ...

Respose : `

{
"posts": [
    {
        "id": 1,
        "titre": "merci",
        "contenu": "des contenus a voir absolument"
    },
    {
        "id": 2,
        "titre": "un autre",
        "contenu": "je suis la"
    }
]

}

You can see, no comments id are in the json. Of course, i have two models with hasMany and belongsTo and sync:true . but is it php-REST responsability to add id comments to the response, or ember data must send another request the get the necessary .?


#4

We just used the PHP WordPress template to gives back JSON :wink: A site made this way: http://www.kerstenconstructie.com/


#5

Hi,

I’ve checked the PHP library you are using, this looks REALLY bad (it is doing POST without validating any data, that’s super dangerous…). In all cases, most REST library do not format responses the way Ember expect it, so you will indeed need to do the joins yourself, and output the identifiers.

If you are using Zend Framework 2, I’m the author of ZfrRest that outputs responses in a format nearly ready-to consume by Ember-Data.


#6

It depends on the Adapter/Serializer you’re using in Ember Data. I think if you’re using DS.ActiveModelSerializer, your back end is expected to provide comment_ids nested in the post json. And if your relationship is set to async:false, then the comment objects are expected to be sideloaded like this:

{
  "comments": [],
  "posts": [
    { "id": 1, "comment_ids": [] }
  ]
}