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

Setting relation between a parent model and child collection #31

Closed
dswitzer opened this issue Sep 23, 2011 · 2 comments
Closed

Setting relation between a parent model and child collection #31

dswitzer opened this issue Sep 23, 2011 · 2 comments

Comments

@dswitzer
Copy link

One thing that keeps tripping me up, is I'm expecting when I set up a reverseRelation that there's also a property on the collection created that references the parent model.

So, if you take this code:

Team = Backbone.RelationalModel.extend({});
TeamList = Backbone.Collection.extend({
    model: Team
});

Person = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone.HasMany // Use the type, or the string 'HasOne' or 'HasMany'.
            , key: 'teams'
            , relatedModel: 'Team'
            , collectionType: 'TeamList'
            , reverseRelation: {
                key: 'person'
            }
        }
    ]
});

What I'd like is for the TeamList collection to contain a reference to the Person model via a "person" property. So if I can something like:

var dan = new Person({name: "Dan"});

I could do:

console.log(dan.get("teams").person.get("name"));

Even if we added an addtional "collectionKey" so that you had to explicitly define a property on the collection level, it would be handy.

The problem I'm running into is I end up binding events at the collection level that need to be aware of the parent Model. So, what I end up doing is creating the relation manually.

The problem with binding it only to the models is that if I need to trigger off an event in the parent model if there are no models in the collection, there's no easy way to do that.

@ulmus
Copy link
Contributor

ulmus commented Sep 30, 2011

What I did was add a collectionOptionsCallback option to the HasMany Relation. That Callback takes the instance being initiated as its only parameter and returns the options-hash used in the initiation of the collection in the other end of the relation. In that the initialization code of the collection, I use the option to, e g reset the rootUrl. I could share the code, but it's a bit tangled into my other modifications. Take a look at my backbone-relational fork and you'll spot it. That might solve it on a more generic level for you.

@singingwolfboy
Copy link
Contributor

I've forked the project, made my own solution to this issue, and sent a pull request: #55

PaulUithol added a commit that referenced this issue Dec 19, 2011
Adding `collectionKey` option; fixes issue #31, fixes issue #55
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

4 participants