Good day! Tell me, please, is it possible to combine models of different collections for certain attributes into one collection?

I am looking for an analogous JOIN operation in SQL, where you can specify the tables and fields by which data is linked.

For example, there are 2 collections Backbone: Children and Parents . Each model in Parents has an ID , each Children model has PARENT_ID . It is necessary to link these collections by the attributes ID and PARENT_ID respectively, in order to obtain the collection of the model of which will contain the attributes of the models of both collections as a result. I will try to show this example:

 //Эти коллекции существуют, берутся из БД var Child = Backbone.Model.extend({ id: some_id, prop1: prop1, prop2: prop2, parent_id: some_parent_id }); var Children = Backbone.Collection.extend({ model: Child }); var Parent = Backbone.Model.extend({ id: some_parent_id, prop3: prop3, prop4: prop4, }); var Parents = Backbone.Collection.extend({ model: Parent }); // Эту нужно получить программно из двух имеющихся var Result = Backbone.Model.extend({ id: some_id, parent_id: some_parent_id prop1: prop1, prop2: prop2, prop3: prop3, prop4: prop4, }); var Results = Backbone.Collection.extend({ model: Result }); 

    1 answer 1

    You can make a model that inherits from Parent (or make Child inherit from Parent if necessary)

     var Result = Parent.extend({ id: some_id, parent_id: some_parent_id prop1: prop1, prop2: prop2 }); 

    To automate model prefilling with data, you can override the constructor:

     constructor: function(parent_id) { var pt = Parents.findWhere({id: parent_id}) this.prop3 = pt.prop3; this.prop4 = pt.prop4; Backbone.Model.apply(this, arguments); } 

    Further, by analogy, as you wish: you can request the data of the Child-model from the server or pull it out of the Children collection