E
E
Evgeny Lebedev2015-06-16 23:07:47
Yii
Evgeny Lebedev, 2015-06-16 23:07:47

Yii2 REST + EmberJS?

I'm trying to make friends with Yii2 as an API with EmberJS.
Everything would be fine, but I'm confused by how Ember and Yii work with model relationships. Yii joins the associated model into a property of the parent, while Ember only wants to see the IDs of the related records in the parent. Essence:
Yii:

'post': {
  'id': 1,
  'title': 'Post title',
  'text': 'some text',
  'comments': [
    {
      'id': 1,
      'title': 'some title',
      'text': 'some text'
    },
    {
      'id': 2,
      'title': 'some title',
      'text': 'some text'
    }
  ]
}

What Ember wants to see:
'post': {
  'id': 1,
  'title': 'Post title',
  'text': 'some text',
  'comments': [1,2]
},
'comments': [
  {
    'id': 1,
    'title': 'some title',
    'text': 'some text'
  },
  {
    'id': 2,
    'title': 'some title',
    'text': 'some text'
  }
]

Which way to look, so that Yii could give data in a convenient Ember format?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stanislav Romanov, 2015-06-17
@HollowJ

If you put async: false in the model field parameters, Ember will wait for the model in the model, as in your first option. But this option is so-so, it's better to look for a ready-made serializer for Yii2 or write your own.

M
Mikhail Osher, 2015-06-17
@miraage

guides.emberjs.com/v1.12.0/models/the-rest-adapter...

D
Dmitry Donkovtsev, 2016-04-15
@Demetriy

Although the answer is no longer very relevant for the one who asked, but since YII + Ember gives this topic when requested, I will write.
To make Yii2 and Ember 2 friends with each other, in the REST plan (so that Ember requests correspond to Yii2 responses), you need to create an adapter and write the following there:

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
    defaultSerializer: 'JSONSerializer',
    host: 'http://адрес_вашего_api'
});

Then everything should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question