Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for polymorphic has-many associations #13

Open
jamesarosen opened this issue Sep 30, 2011 · 2 comments
Open

Add support for polymorphic has-many associations #13

jamesarosen opened this issue Sep 30, 2011 · 2 comments

Comments

@jamesarosen
Copy link
Collaborator

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
Copy link
Collaborator Author

The same probably applies for has-one associations:

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

@jamesarosen
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant