本文概述
- 参数说明
句法:
collection.create(attributes, options)
参数说明属性:它指定已定义模型的属性。
【backbone.js集合create】options:它指定参数(例如id,name等)来创建集合实例。
让我们以一个示例来部署create方法。
请参阅以下示例:
<
!DOCTYPE html>
<
head>
<
title>Create Collection Example<
/title>
<
script src="http://img.readke.com/220416/06102SP0-0.jpg" type="text/javascript"><
/script>
<
script src="http://img.readke.com/220416/06102V030-1.jpg" type="text/javascript"><
/script>
<
script src="http://img.readke.com/220416/06102SP7-2.jpg" type="text/javascript"><
/script>
<
/head>
<
body>
<
script type="text/javascript">
//The model 'ModelDemo' uses the sync method to display the model state such as create, read, update etc
var ModelDemo = Backbone.Model.extend({
sync : function (method, model, options) {
document.write(JSON.stringify(arguments));
}
});
//'CollectionDemo' is an instance of the collection
var CollectionDemo = Backbone.Collection.extend({
model : ModelDemo//The model 'ModelDemo' is specified by overriding the 'model' property
});
//'ViewDemo' is the name of the view
var ViewDemo = Backbone.View.extend({//The instance of the collection 'collectiondemo' is created within the 'initialize()' function
initialize : function () {
var collectiondemo = new CollectionDemo();
collectiondemo.create({
Name:"Narendra Modi", Country:"India"
});
}
});
new ViewDemo();
//The view instance 'ViewDemo' is created using the 'new' keyword.
<
/script>
<
/body>
<
/html>
输出:
将以上代码保存在create.html文件中,然后在新的浏览器中打开该文件。
文章图片
推荐阅读
- backbone.js路由教程
- backbone.js集合fetch
- backbone.js集合clone
- backbone.js集合parse
- backbone.js集合url
- backbone.js集合findwhere
- backbone.js集合where
- backbone.js集合pluck
- backbone.js集合排序sort