staugaard / ember-resource

This project has moved. The canonical repository is now located at: https://github.com/zendesk/ember-resource
https://github.com/zendesk/ember-resource
Apache License 2.0
122 stars 27 forks source link

Add support for polymorphic has-many associations #13

Open jamesarosen opened 13 years ago

jamesarosen commented 13 years ago

It's possible to "fake" a polymorphic association by using a "class" whose constructor is really a factory method for other classes. Unfortunately, this falls short when the different classes have different parse methods. I suggest the following syntax:

MyBlog.Post = SC.Resource.define({
  comments: {
    type: SC.ResourceCollection,
    itemType: function(json) { return SC.get('MyBlog.%@'.fmt(json.type)); },
    url: '/posts/%@/comments'
  }
})

If itemType is a function, it is called with each of the data objects in turn to get the parser and constructor.

jamesarosen commented 13 years ago

The same probably applies for has-one associations:

latestComment: {
  type: function(json) { return SC.get('MyBlog.%@'.fmt(json.type)); },
  url: '/posts/%@/latestComment'
}
jamesarosen commented 13 years ago

The syntax above isn't sufficient. The problem is that there's no way to distinguish between

latestComment: {
  type: function(json) { return SC.get('MyBlog.%@'.fmt(json.type)); },
  url: '/posts/%@/latestComment'
}

and

latestComment: {
  type: MyBlog.Comment,
  url: '/posts/%@/latestComment'
}

since MyBlog.Comment is also a function.

Perhaps we'll need to add polymorphic: true to the definition.