getoutreach / epf

A framework for keeping your Ember.js apps in sync.
http://epf.io
MIT License
369 stars 33 forks source link

deserializeEmbeddedBelongsTo tries to createModel() with a string... #11

Closed tobiaswerner closed 11 years ago

tobiaswerner commented 11 years ago

I have problems deserializing embedded relationships.

Up to now I could figure out that deserializeEmbeddedBelongsTo() which calls deserializeModel receives a string as type. deserializeModel() in turn tries to .create() the model but still fail (because it is typeof string).

I configured the RestAdapter to always embed the one model in the other one..

Ep.RestAdapter.map('App.Gear', {
     user: { embedded: 'always' }
});

Did I miss some configuration?

jasonkriss commented 11 years ago

Can you post your model code for Gear and User?

tobiaswerner commented 11 years ago

Sure. Here it is...

var attr = Ep.attr,
    belongsTo = Ep.belongsTo,
    hasMany = Ep.hasMany;

App.Gear = Ep.Model.extend({
    user: belongsTo('App.User'),
        manufacturer: belongsTo('App.Manufacturer'),
    description: attr('string')
});
var attr = Ep.attr,
    belongsTo = Ep.belongsTo,
    hasMany = Ep.hasMany;

App.User = Ep.Model.extend({
  name: attr('string'),
  gears: hasMany('App.Gear')  
});

Nothing special so far, I think... Thanks for your help in advance :)

jasonkriss commented 11 years ago

Without testing it out myself, I think is this is probably related to #4. EPF doesn't yet support defining relationships via strings, so try declaring your models like this:

App.Gear = Ep.Model.extend({
  description: attr('string')
});
App.Manufacturer = Ep.Model.extend({
  gears: hasMany(App.Gear)
});
App.User = Ep.Model.extend({
  name: attr(string),
  gears: hasMany(App.Gear)  
});
App.Gear.reopen({
  user: belongsTo(App.User),
  manufacturer: belongsTo(App.Manufacturer)
});

You need to use reopen whenever there is a circular dependency. Take a look at #4 though as this won't be necessary in the future.

tobiaswerner commented 11 years ago

Perfect, many thanks for your explanation. Will try it tomorrow...

ghempton commented 11 years ago

I think @jasonkriss is correct. I will try and get #4 fixed soon. I'm going to close this for now, feel free to reopen if that isn't the fix.

tobiaswerner commented 11 years ago

Your hints solved my problem. Many thanks :+1: