本文概述
- 参数说明
句法:
collection.parse(response, options) )
参数说明响应:它指定集合的??模型属性的数组。
options:如果设置为true,则以JSON格式表示数据。
让我们以一个示例来演示解析方法。
请参阅以下示例:
<
!DOCTYPE html>
<
head>
<
title>Parse Collection Example<
/title>
<
script src="http://img.readke.com/220416/052TQ624-0.jpg" type="text/javascript"><
/script>
<
script src="http://img.readke.com/220416/052TV359-1.jpg" type="text/javascript"><
/script>
<
script src="http://img.readke.com/220416/052TQ443-2.jpg" type="text/javascript"><
/script>
<
/head>
<
body>
<
script type="text/javascript">
//'MyModel' is a model name andextended using the Backbone.Model class
var MyModel = Backbone.Model.extend();
// The variable 'myData' contains the values which are need to be parsed in the collection
var myData =http://www.srcmini.com/{"values": [{
"fname": "Saurav", "lname": "Ganguly", "country": "India"
}]
};
//'MyCollection' is a collection name
var MyCollection = Backbone.Collection.extend({
model: MyModel, //The model 'MyModel' is specified by overriding the 'model' property
parse : function(response, options){
document.write(JSON.stringify(response));
}
});
//The collection instance 'myCollection' extracts the values of 'myData'only if parse is set to true
var mycollection = new MyCollection(myData, { parse: true });
<
/script>
<
/body>
<
/html>
输出:
【backbone.js集合parse】将以上代码保存在parse.html文件中,然后在新的浏览器中打开此文件。
文章图片
推荐阅读
- backbone.js集合url
- backbone.js集合findwhere
- backbone.js集合where
- backbone.js集合pluck
- backbone.js集合排序sort
- backbone.js集合比较器comparator
- backbone.js集合length
- backbone.js集合slice
- backbone.js集合shift