本文概述
Backbone.js集合的where方法通过使用集合中的match属性来检索和显示模型。
句法:
collection.where(attribute)
参数说明
属性:它指定已定义模型的属性。
让我们以一个示例来部署where方法。
请参阅以下示例:
<!DOCTYPE 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 players = new Backbone.Collection([
{name: "Sachin", job: "Cricketer"}, {name: "Dhoni", job: "Cricketer"}, {name: "Bradman", job: "Cricketer"}, {name: "Ronaldo", job: "Footballer"}, ]);
var Cricketer = players.where({job: "Cricketer"});
document.write("Total numbers of items that matches given attribute are:", +Cricketer.length);
</script>
</body>
</html>
输出:
将以上代码保存在where.html文件中,并在新的浏览器中打开该文件。
评论前必须登录!
注册