BackboneJS - Collection Reset
描述
它将重置集合并用新的模型数组填充,或者将清空整个集合。
语法
collection.reset(models,options)
参数
models − 它包含集合中需要重置的集合实例的名称。
options − 选项包括用于清空集合的空值。
示例
<!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">
//'C_Name' 是模型名称,包含默认值
var C_Name = Backbone.Model.extend ({
defaults: {
country: "sachin"
}
});
//'PlayersCollection' 是集合的实例,模型 'C_Name' 通过使用
//model 属性指定
var PlayersCollection = Backbone.Collection.extend ({
model: C_Name
});
//'country1' 和 'country2' 是模型 'C_name' 的实例
var country1 = new C_Name({country: "australia"});
var country2 = new C_Name({country: "england"});
//使用 'mycollection' 集合实例将模型实例添加到集合中
var mycollection = new PlayersCollection();
mycollection.add([country1,country2]);
//'length' 属性定义集合的长度
document.write('添加的国家/地区数:' + mycollection.length);
document.write("<br>");
//此处,reset() 方法重置集合,否则将清空集合
mycollection.reset();
document.write('重置后的国家/地区数:' + mycollection.length);
</script>
</body>
</html>
输出
让我们执行以下步骤来查看上述代码的工作原理 −
将上述代码保存在 reset.html 文件中。
在浏览器中打开此 HTML 文件。