Can Backbone render a collection in reverse order?

后端 未结 8 1543
猫巷女王i
猫巷女王i 2021-02-09 04:19

I\'m using a Signalr hub to subscribe to events on the server. What an event is dispatched to a hub, its successfully adding the item to a Marionette CollectionView. This, in

8条回答
  •  盖世英雄少女心
    2021-02-09 04:34

    As the BackBone doesn't support reverse iteration of the collection (and it's just waste of resources to reverse or worse sort the collection) the easiest and fastest approach is to use the for loop with decreasing index over models in the collection.

    for (var i = collection.length - 1; i >= 0; i--) {
        var model = collection.models[i];
    
        // your logic
    }
    

    It's not that elegant as sorting or reversing the collection using Underscore but the performace is much better. Try to compare different loops here just to know what costs you to write foreach instead of classic for.

提交回复
热议问题