Implement many-to-many relationships with basic relational

I have a simple application that defines two classes: a Person and a PersonGroup , in which there is a many-to-many relationship. A person cannot have any group or be assigned to all groups and everything in between.

The example at backbonerelational.org suggests using an intermediate model for many-to-many relationships, however I cannot get this template to work with extraction (deserialization) and preservation (serialization).

What I want to do is use Backbone to deserialize JSON, similar to the following:

 { People: [ { "ID": 1, "Name": "John" }, { "ID": 2, "Name": "Bob" }, { "ID": 3, "Name": "Tim" }, ], PeopleGroups: [ { "ID": 1, "Name": "Owners", "People": [ 1, 2 ], }, { "ID": 2, "Name": "Everyone", "People": [ 1, 2, 3 ], }, ] } 

I use Knockback / Knockout to bind data, so the problem is that I need to have access to relationships by reference. An identifier array does not do me any good if I cannot create Knockback.CollectionObservable to wrap the collection and allow identifiers for links.

+6
source share
2 answers

I ended up working the way I wanted. Many-to-many relationships are maintained in the database, but relationships can only be accessed in my Backbone models in one direction. I decided that the Person models would be self-contained, and the PersonGroup models would have a set of references to the Person models to which they are attached.

The key points at which everything was done was to specify includeInJSON: "ID" and remove reverseRelation . This still allows you to access model references in JavaScript, but it is correctly serialized and deserialized in JSON. Person models simply do not have access to the navigation properties of the groups in which they are located, however they can exist in several groups just fine.

I just assumed that using a list of identifiers would make it possible to jump over hoops to resolve links, but Backbone-relational seems to use the Backbone global model repository to resolve links by ID without creating duplicate models. (for example, three different groups can refer to the same Person, and only one model is ever created).

 var Person = Backbone.RelationalModel.extend( { idAttribute: "ID", }); var PersonGroup = Backbone.RelationalModel.extend( { idAttribute: "ID", relations: [ { type: Backbone.HasMany, key: "People", relatedModel: "Person", collectionType: "PersonCollection", includeInJSON: "ID", }, ], }); 

And here is the rest of the plumbing, if it helps anyone. You can define Backbone.Collections as follows and get them through separate API requests:

 var PersonCollection = Backbone.Collection.extend( { model: Person, url: "/api/person", }); var PersonGroupCollection = Backbone.Collection.extend( { model: PersonGroup, url: "/api/persongroup", }); var PersonModels = new PersonCollection(); var GroupsModels = new PersonGroupCollection(); this.PersonModels.fetch(); this.GroupsModels.fetch(); this.People = kb.collectionObservable( this.PersonModels, { factories: { "models": PersonViewModel, }, } ); this.PersonGroups = kb.collectionObservable( this.GroupsModels, { factories: { "models": PersonGroupViewModel, "models.People.models": PersonViewModel, }, } ); 

I have included specific Knockback.js collections to use Knockout.js bindings. Only one ViewModel is created for each model, so change tracking is distributed throughout the application.

+7
source

This question is about two months old, however, I would like to share that I encountered similar problems related to many-to-many relationships when working with Backbone-relational. These problems led to the writing of their own implementation of basic relationships that support many-to-many relationships without the relationship between the relationship model: Backbone-JJRelational

I answer as one of the authors, but maybe worth checking out.
For example, the JSON that you posted in your question will automatically be deserialized as follows:
Each PeopleGroups mod has an attribute People , which is a Backbone.Collection containing the corresponding People models and (of course) vice versa, therefore the relation can be easily accessed from both sides (without model binding).
Some Backbone-JJRelational concepts are similar to Backbone-relational (e.g. global model repository).

+3
source

Source: https://habr.com/ru/post/950084/


All Articles