backbone.js集合排序sort

本文概述

  • 参数说明
在Backbone.js中,排序收集方法用于对集合中的项目进行排序。它与比较器属性一起使用,以便对项目进行排序。
句法:
collection.sort(options)

参数说明options:它指定两个参数“ true”或“ false”以启用或禁用排序。
让我们以一个示例来部署排序收集方法。
请参阅以下示例:
< !DOCTYPE html> < head> < title>Sort Collection Example< /title> < script src="http://img.readke.com/220416/052JI094-0.jpg" type="text/javascript">< /script> < script src="http://img.readke.com/220416/052JM2B-1.jpg" type="text/javascript">< /script> < script src="http://img.readke.com/220416/052JMH9-2.jpg" type="text/javascript">< /script> < /head> < body> < script type="text/javascript"> var Player = Backbone.Model.extend(); //'Player' is a model name//Players names will be displayed in ascending order. var Players = [ {player: 'Ronaldo', id: '12'}, {player: 'Pele', id: '32'}, {player: 'Neymar', id:'18'} ]; //'myteam' is a collection instance and model 'Player' is specified using model property var myteam = new Backbone.Collection(Players, { model:Player, //The comparator method is used to maintain the collection in sorted order comparator: 'player' }); //display the sorted records, records are sorted based on player as we have set comparator to 'player' document.write("The sorted items are: ", JSON.stringify(myteam.toJSON())); < /script> < /body> < /html>

输出:
【backbone.js集合排序sort】将以上代码保存在sort.html文件中,然后在新的浏览器中打开该文件。
backbone.js集合排序sort

文章图片

    推荐阅读