
- BackboneJS 教程
- BackboneJS - 主頁
- BackboneJS - 概述
- BackboneJS - 環境設定
- BackboneJS - 應用程式
- BackboneJS - 事件
- BackboneJS - 模型
- BackboneJS - 集合
- BackboneJS - 路由
- BackboneJS - 歷史記錄
- BackboneJS - 同步
- BackboneJS - 檢視
- BackboneJS - 實用程式
- BackboneJS 有用資源
- BackboneJS - 快速指南
- BackboneJS - 資源
- BackboneJS - 討論
BackboneJS - 集合排序
說明
集合排序命令對集合中的項進行排序,並使用 comparator 屬性對項進行排序。
語法
collection.sort(options)
引數
選項 − 其中包含可選引數,例如 True 或 False,以停用或啟用排序。
示例
<!DOCTYPE html> <html> <head> <title>Collection Example</title> <script src = "https://code.jquery.com/jquery-2.1.3.min.js" type = "text/javascript"></script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type = "text/javascript"></script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type = "text/javascript"></script> </head> <body> <script type = "text/javascript"> var Player = Backbone.Model.extend(); //'Player' is a model name //The variable 'Players' contains the items which are to be displayed in the ascending order var Players = [ {player: 'sachin',id: '44'}, {player: 'ganguly',id: '22'}, {player: 'dhoni',id:'33'} ]; //'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>
輸出
讓我們執行以下步驟,瞭解上述程式碼的工作原理 -
將上述程式碼儲存在 sort.htm 檔案中
在瀏覽器中開啟此 HTML 檔案。
backbonejs_collection.htm
廣告