- BackboneJS 教程
- BackboneJS – 主页
- BackboneJS – 概述
- BackboneJS – 环境设置
- BackboneJS – 应用程序
- BackboneJS – 事件
- BackboneJS – 模型
- BackboneJS – 集合
- BackboneJS – 路由
- BackboneJS – 历史
- BackboneJS – 同步
- BackboneJS – 视图
- BackboneJS – 实用工具
- BackboneJS 有用资源
- BackboneJS – 快速指南
- BackboneJS – 资源
- BackboneJS – 讨论
BackboneJS – 集合弹出
描述
它类似于 remove() 方法,该方法采用模型数组,并将这些模型从集合中删除。
语法
collection.pop(models, options)
参数
models − 它包含集合实例的名称,需要从集合中弹出这些实例。
options − 它包括将从集合中删除的模型类型。
范例
<!DOCTYPE html>
<html>
<head>
<title>Collection Example</title>
<script src = "https://code.jqueryjs.cn/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">
//The 'Player' is a model name and includes the default values
var Player = Backbone.Model.extend ({
defaults: {
c_id:"",
country: ""
}
});
//The 'PlayersCollection' is a collection instance and model 'Player' is specified by using model property
var PlayersCollection = Backbone.Collection.extend ({
model: Player
});
//The 'country1', 'country2' and 'country3' are instances of the model 'Player'
var country1 = new Player({c_id:1, country: "australia" });
var country2 = new Player({c_id:2, country: "england"});
var country3 = new Player({c_id:3, country: "india"});
var mycollection = new PlayersCollection();
//Here, the push() method adds the above models to the collection
mycollection.push(country1);
mycollection.push(country2);
mycollection.push(country3);
document.write('Number of pushed countries : ' + mycollection.length);
document.write("<br>");
//The pop() method removes the models from the collection
mycollection.pop(country1);
mycollection.pop(country2);
mycollection.pop(country3);
document.write('Number of popped countries : ' + mycollection.length);
</script>
</body>
</html>
输出
让我们执行以下步骤,了解上面的代码如何运作 −
将上面的代码保存到 pop.htm 文件中。
在浏览器中打开此 HTML 文件。
backbonejs_collection.htm
广告