getoutreach / epf

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

Defining relationships with strings #4

Closed mitchlloyd closed 11 years ago

mitchlloyd commented 11 years ago

I noticed that your examples define relationships by passing the actual model classes as arguments.

App.Post = Ep.Model.extend({
  comments: Ep.hasMany(App.Comment),
  user: Ep.belongsTo(App.User)
});

This is different than the Ember Data examples that define relationships with strings like this.

App.Post = DS.Model.extend({
  comments: DS.hasMany('App.Comment'),
  user: DS.belongsTo('App.User')
});

Can relationships be created using strings to represent the related models?

ghempton commented 11 years ago

String support is coming soon, but it will be a slightly different flavor than Ember Data. Epf will use container strings:

App.Post = Ep.Model.extend({
  comments: Ep.hasMany('comment'),
  user: Ep.belongsTo('user')
});

This already works with sessions. E.g. session.load('post', 1), but not quite yet with relationships. To work around this in the short term you should reopen the class when you have circular dependencies:

App.Post = Ep.Model.extend();
App.Comment = Ep.Model.extend({
  post: Ep.belongsTo(App.Post)
});
App.Post.reopen({
  comments: Ep.hasMany(App.Comment)
});

Hope this helps.

mitchlloyd commented 11 years ago

Thanks for the workaround. I'm really excited to try Epf out!

ghempton commented 11 years ago

This should work now. E.g. Ep.belongsTo('post') or Ep.hasMany('comment'). A common typo might be to use the plural for Ep.hasMany, but the singular should always be used for the time being.

lastobelus commented 11 years ago

camel case or underscore?

ghempton commented 11 years ago

It follows the same naming conventions as ember proper. I think it normalizes both camel case and underscore.