backbone.js集合at

本文概述

  • 参数说明
Backbone.js集合at方法用于通过使用指定的索引从集合中检索模型。
句法:
index()

参数说明index:它为可以从集合中检索模型的索引位置进行分类。
让我们举个例子。
【backbone.js集合at】请参阅以下示例:
< !DOCTYPE html> < head> < title>At Collection Example< /title> < script src="http://img.readke.com/220416/043932HY-0.jpg" type="text/javascript">< /script> < script src="http://img.readke.com/220416/0439321404-1.jpg" type="text/javascript">< /script> < script src="http://img.readke.com/220416/04393264I-2.jpg" type="text/javascript">< /script> < /head> < body> < script type="text/javascript"> //The model name is 'Player' and contains default values var Player = Backbone.Model.extend({ defaults: { id:"", name: "" } }); //'PlayersCollection' is an instance of the collection var PlayersCollection = Backbone.Collection.extend({ model: Player//model 'Player' is specified by using model property }); var player1 = new Player({id:1, name: "Dravid" }); var player2 = new Player({id:2, name: "Ganguly"}); //The add() method adds the models 'player1' and 'player2' to the collection instance 'mycollection' var mycollection = new PlayersCollection(); mycollection.add([player1, player2]); document.write('< b>Players added are :< /b> ' + JSON.stringify(mycollection.toJSON())); var player3 = new Player({id:3, name: "Sehwag" }); //Here, adding the model 'player3' at 0th index of the collection mycollection.add(player3, {at:0}); //display all the models added. player3 will be added at the 0th position document.write('< br>< b>Now the new list of players is :< /b> ' + JSON.stringify(mycollection.toJSON())); < /script> < /body> < /html>

输出:
将上面的代码保存在at.html文件中,然后在新的浏览器中打开该文件。
backbone.js集合at

文章图片

    推荐阅读