Reverse sort order with Backbone.js

前端 未结 15 989
不知归路
不知归路 2020-12-02 05:45

With Backbone.js I\'ve got a collection set up with a comparator function. It\'s nicely sorting the models, but I\'d like to reverse the order.

How can I sort the m

15条回答
  •  天涯浪人
    2020-12-02 06:31

    Here is a really simple solution, for those who simply want to flip the current order. It is useful if you have a table whose columns can be ordered in two directions.

    collection.models = collection.models.reverse()
    

    You can combine it with something like this if you are interested in the table case (coffeescript) :

      collection.comparator = (model) ->
        model.get(sortByType)
    
      collection.sort()
    
      if reverseOrder
        collection.models = collection.models.reverse()
    

提交回复
热议问题