Answer the question
In order to leave comments, you need to log in
Backbone how to load recursive data?
Can you please tell me the best way to organize recursive data loading in the backbone, with an unlimited number of nestings?
Something like a warehouse:
Category 1
- Products (product 1, product 2),
- Subcategory 1.1
- - products ( product3, product4)
- Subcategory 1.2
- - products ( product5, product6)
Category 2
- Products (product 7, product 8 ),
Answer the question
In order to leave comments, you need to log in
var Item = Backbone.Model.extend({
defaults: {
items: [],
parent: null //Ссылка на родительский элемент
},
initialize: function(){
this.set('items', new ItemsCollection(this.get('items')));
this.get('items').each(function(){ this.set('parent', this) }, this);
}
});
var ItemsCollection = Backbone.Collection.extend({
model: Item,
//...
});
var collection = new ItemsCollection();
collection.fetch();
Backbone Relational mega-brake.
I described with my hands the connections between nested collections and the global one. Each local collection populates itself from the global collection during initialization.
If there are not many records (less than a thousand), you can simply load them into collections, and get the amount that you need while collecting the tree on the fly during rendering.
Usually, the first level of nodes is pulled out first, and when you try to open it, the second level is dynamically connected. This is done through the model's fetch method. All that is required is to pass the ID of the parent category.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question