Batman.js doesn’t support
hasManyThrough out of the box, but it can be implemented fairly easily with
This feature was just merged into the master branch – download the latest batman.js here.
What’s a “Has-Many-Through” Association?
It’s best shown by example. To join
Person, you might have a “join model”,
HouseholdMembership. The associations look like this:
1 2 3 4 5 6 7 8 9
hasMany memberships, each membership
belongsTo a person.
1 2 3 4 5
Household has many people through household memberships.
Has-Many-Through in Batman.js
hasManyThrough isn’t part of batman.js, you can implement a read-only has-many-through using
Set::mappedTo. Given classes as defined above, you could add an accessor for
This returns a
Batman.Set (actually a
Batman.SetMapping) containing unique
Persons belonging to those
householdMemberships. As batman.js does, items added and removed are automatically tracked, so this is safe to use everywhere.
As for adding items, you could do it this way:
1 2 3
Batman.SetMapping will take care of keeping everything in sync!