Iterating assembly of supports

I set the base collection for Users , and when I execute the fetch method, I return the JSON object line by line: {"users": [{...}, {...}, ...], size: number} from the server . Vaguely, when I execute the code below, instead of getting each user object, I get a single "child" object that has two attributes : users and size; can anyone help me understand why? Thanks.

 display: function(){ this.collection.each(function(user){ console.log("main", user); }); } 
+6
source share
2 answers

Add a method to the collection called parse:

 var collection = new Backbone.Collection({ parse: function(response) { return response.users; } }); 
+7
source

That makes sense to me. Look at JSON: it has two properties: users and size.

You probably just want to iterate over collection.users :

 display: function(){ this.collection.users.each(function(user){ console.log("main", user); }); } 

Instead, instead of foo instead of foo ( foo ), instead of collection ( foo ), an object is created created by parsing the returned JSON).

+4
source

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


All Articles