1

ember, ember-data 1.0, rails, rabl

I have next json returning from server:


    {
     "day":{
       "id":5,   
       "expenditures":[{
           "id":10,
           "expense_type":{
             "name":"Very Sad",
             "id":2
           }
         }, 
         {...}
       ]
     }
    }

I have next ember models:


    Expense.Day = DS.Model.extend 
      expenditures: DS.hasMany('expenditure')

    Expense.Expenditure = DS.Model.extend 
      day: DS.belongsTo('day')
      expenseType: DS.belongsTo('expenseType')

    Expense.ExpenseType = DS.Model.extend 
      name: DS.attr('string')
      expenditures: DS.hasMany('expenditure')

And I use ActiveModelSerializer for each model with EmbeddedRecordsMixin, for ex:


    Expense.DaySerializer = Expense.ApplicationSerializer.extend DS.EmbeddedRecordsMixin,
      attrs:
        expenditures: {embedded: 'always'}

    Expense.ExpenditureSerializer = Expense.ApplicationSerializer.extend DS.EmbeddedRecordsMixin,
      attrs:
        expenseType: {embedded: 'always'}
        day: {embedded: 'always'}

    Expense.ExpenseTypeSerializer = Expense.ApplicationSerializer.extend DS.EmbeddedRecordsMixin,
      attrs:
        expenditures: {embedded: 'always'}

It propertly loads day and expenditures, but not expense_type. I inject into each serializer merhod extract with console.log and super() for debugging and see, that only DaySerializer executed. What's wrong with me? I am very close to insanity with Ember =(

real_ate
  • 10,861
  • 3
  • 27
  • 48
Arugin
  • 1,931
  • 2
  • 13
  • 17
  • 1
    I can see that your server is giving you an `expense_type` when you expect an `expenseType`. Or is this only a mistake on SO ? Anyway, you should use camelcased property as this is what ember-data expect. – Bartheleway Jul 08 '14 at 11:43
  • I used ActiveModelSerializer and Adapter, which should (I mistaken?) automaticly convert snak_case to camelCase. – Arugin Jul 08 '14 at 12:46
  • You're right, I didn't pay attention to that. Sorry. Could you try adding `async: false` to your model relationship definition ? (It's just an idea). – Bartheleway Jul 08 '14 at 13:18
  • Nope :(. The problem, I think, is that ember don't execute Serializer for Expenditure and incorrectly extract expenseType relation. – Arugin Jul 08 '14 at 18:00

1 Answers1

1

The problem is for my ember data understanding. I look into the EmbeddedRecordsMixin source code and see, that only hasMany ralation can be embedded.

Just adding expense_type_id node to expenditure item solves the problem:

{
 "day":{
   "id":5,   
   "expenditures":[{
       "id":10,
       "expense_type_id": 2
     }, 
     {...}
   ]
 }
}

Ember process GET request for /expense_types/2 and load model. Thats all!

Arugin
  • 1,931
  • 2
  • 13
  • 17