简体   繁体   中英

iterate through an array in a collection

So, I'm still very new to Backbone. My question is this: I have a collection that is made like this:

...
isCollection: true,
parse: function(response) {
    if (response.status == 'ok') {
        this.page = response.result.page;
        this.count = response.result.count;
        this.total = response.result.total;
        this.sort = response.result.sort;
        this.ascending = response.result.ascending;
        this.myReports = response.result.results;
        return this;
    } else {

    }

Where this.myReports is an array of objects. My question is how do I iterate over the array (myReports)? Do I have to convert it to a collection? If I try to use .each, i get an error about .each not being supported.

Thanks for any help!

Did you try iterating through it like a simple js array?

something like:

for(var i in myArray){
   var item = myArray[i];
   //dosomething
}

Maybe it's worth trying underscorejs for advanced array features.

I would put the elements of response.result.results into the collection this way:

parse: function(response) {
    if (response.status == 'ok') {
        this.page = response.result.page;
        this.count = response.result.count;
        this.total = response.result.total;
        this.sort = response.result.sort;
        this.ascending = response.result.ascending;
        return response.result.results;
    } else {

    }
}

Now if fetch ing the collection succeeds, and response.status is 'ok' , then:

  • your collection will have fields called page , count , total , sort and ascending ,
  • these fields will contain the values you get as the fields of response.result ,
  • the length of the collection will be the length of response.result.results , and
  • the items of the collection will be the items of response.result.results , so you can iterate over them inside the success handler of fetch .

If the fetch fails, or response.status is not 'ok' , then the length of the collection will be zero. The fields page , count , total , sort and ascending will be undefined , unless you define them somewhere, for example in initialize or in the else branch inside the above parse function.

Here is a working example. The output is printed in the console, so eg in Chrome, press F12 and change to the Console tab. Anyway, since Backbone collections have a method called sort , I would change the name of the sort field to something else ( sorted , toBeSorted , etc. - I don't know the semantics of this field).

When you call myCollection.each on a Backbone collection, you are essentially calling Underscore's each method, but it is being presented to you a bit nicer as a Backbone.Collection method.

You could just use the method on the array:

_.each(this.myReports, function (report) {
  // do whatever with report
})

Whether you should convert it to a backbone collection is another question. If you want to store models in it, or use any of the Backbone.Collection functionality, then sure, convert it using the each method above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM