You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
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:
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:
I could do:
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.
The text was updated successfully, but these errors were encountered: