【backbone.js集合clone】Backbone.js集合克隆方法返回具有相同模型列表的集合的新实例。
句法:
collection.clone()
让我们以一个示例来演示克隆方法。
请参阅以下示例:
<
!DOCTYPE html>
<
head>
<
title> Clone Collection Example<
/title>
<
script src="http://img.readke.com/220416/06100435N-0.jpg" type="text/javascript"><
/script>
<
script src="http://img.readke.com/220416/0610042Y3-1.jpg" type="text/javascript"><
/script>
<
script src="http://img.readke.com/220416/0610041918-2.jpg" type="text/javascript"><
/script>
<
/head>
<
body>
<
script type="text/javascript">
//'Person' is a model name
var Person= Backbone.Model.extend();
//The model instance 'person' contains 'name' attribute
var person=new Person({
name: 'Chris Martin'
});
var MyCollection = Backbone.Collection.extend({
model: Person//model is override by specifying the "model" property of collection class
});
var myCollection = new MyCollection();
// The clone() method uses get method to retrieve the 'name' attribute
var details = myCollection.clone(person.get('name'));
//Use a variable 'details' to assign the value for 'name' as 'John Davison'
details.name="John Davison";
document.write("The new instance of collection is: ", JSON.stringify(details.name));
<
/script>
<
/body>
<
/html>
输出:
将以上代码保存在clone.html文件中,然后在新的浏览器中打开此文件。
文章图片
推荐阅读
- backbone.js集合fetch
- backbone.js集合parse
- backbone.js集合url
- backbone.js集合findwhere
- backbone.js集合where
- backbone.js集合pluck
- backbone.js集合排序sort
- backbone.js集合比较器comparator
- backbone.js集合length