在Backbone中常用的Underscore函数

无主题博客 » 前端开发 » Backbone » 在Backbone中常用的Underscore函数

1.forEach:迭代集合

var MyCollection = new Backbone.Collection();
MyCollection.add([
  {name:'A'},
  {name:'B'},
  {name:'C'}
]);

MyCollection.forEach(function(model){
  console.log(model.get('name'));
});
//Log:
//A
//B
//C

2.sortBy():通过特定的属性对集合进行排序

var MyCollection = new Backbone.Collection();
MyCollection.add([
  {name:'B'},
  {name:'A'},
  {name:'C'}
]);

var sortedByMyCollection = MyCollection.sortBy(function (model) {
 return model.get("name").toLowerCase();
});

sortedByMyCollection.forEach(function(model){
 console.log(model.get('name'));
});
//Log:
//A
//B
//C

3.map():通过转换函数映射列表里得每个项,重新生成一个新集合

var myCollection = new Backbone.Collection([
 {name:'A'},
 {name:'B'}
]);
var i=1;
console.log(myCollection.map(function(model){
  return i++ + ". " + model.get("name");
}));
//["1. A", "2. B"]

4.min()/max():获取特定属性为最小/最大值的model项

var myCollection = new Backbone.Collection([
 {id:1},
 {id:2}
]);
console.log(myCollection.max(function(model){
  return model.id;
}).id); //2

console.log(myCollection.min(function(model){
  return model.id;
}).id); //1

5:pluck():获取特定属性的集合

var myCollection = new Backbone.Collection([
 {id:1},
 {id:2}
]);
var ids = myCollection.pluck('id');
console.log(ids);//[1 ,2]

6:filter():过滤集合

通过一组模型ID形式的数组进行过滤。

var MyModel = Backbone.Model.extend({});
var myCollection = Backbone.Collection.extend({
 model:MyModel,
 filterById: function(ids){
   return this.models.filter(
     function(c) {
       return _.contains(ids, c.id)
     }
   );
 }
});

7:indexOf():返回集合中特定模型的索引位置

var MyModel = Backbone.Model.extend({});
var MyCollection = Backbone.Collection.extend({
  model:MyModel
});
var m1 = new MyModel({id:1}),
    m2 = new MyModel({id:2});
var myCollection = new MyCollection([m1, m2]);

console.log(myCollection.indexOf(m1));//0
console.log(myCollection.indexOf(m2));//1

8.any():通过迭代器测试集合中是否存在特定的模型

var MyModel = Backbone.Model.extend({});
var MyCollection = Backbone.Collection.extend({
  model:MyModel
});
var m1 = new MyModel({id:1});
var myCollection = new MyCollection([m1]);
console.log(myCollection.any(function(model){
  return model.id === 2;
}));//false

console.log(myCollection.any(function(model){
  return model.id === 1;
}));//true

9.size():返回集合的大小

var myCollection = new Backbone.Collection([
  {name:'A',age:3},
  {name:'B',age:2},
  {name:'C',age:2}
]);
console.log(myCollection.size());

10.isEmpty():判断集合是否为空

var myCollection = new Backbone.Collection();
var isEmpty = myCollection.isEmpty();
console.log(isEmpty);//true
myCollection.add({name:'A'});
isEmpty = myCollection.isEmpty();
console.log(isEmpty);//false
11.groupBy():通过模型的属性将集合进行分组
var myCollection = new Backbone.Collection();
myCollection.add([
  {name:'A',age:3},
  {name:'B',age:2},
  {name:'C',age:2}
]);
var byAge = myCollection.groupBy('age');
var age = new Backbone.Collection(byAge[2]);
console.log(age.pluck('age')); //[2, 2]

12.pick():过滤出模型特定属性的属性值

var MyModel = Backbone.Model.extend({});
var myModel = new MyModel({
  title:'A',
  desc:'我是模型A'
});
console.log(myModel.pick('title'));//Object {title: "A"}

13.omit():过滤出模型特定属性以外的属性值

var MyModel = Backbone.Model.extend({});
var myModel = new MyModel({
  title:'A',
  desc:'我是模型A'
});
console.log(myModel.omit('title'));//Object {desc: "我是模型A"}

14.keys()与values():获取一个对象的所有属性名称/属性值

var MyModel = Backbone.Model.extend({});
var myModel = new MyModel({
  title:'A',
  desc:'我是模型A'
});

console.log(myModel.keys());//["title", "desc"]
console.log(myModel.values());//["A", "我是模型A"]

15.pairs():把一个对象转变为[key, value]形式的数组

var MyModel = Backbone.Model.extend({});
var myModel = new MyModel({
  title:'A',
  desc:'我是模型A'
});
var pairs = myModel.pairs();
console.log(pairs[0]);//["title", "A"]
console.log(pairs[1]);//["desc", "我是模型A"]

16.invert():将一个对象的键(keys)和值(values)互换后创建一个新对象

var MyModel = Backbone.Model.extend({});
var myModel = new MyModel({title:'A'});
console.log(myModel.invert());//Object {A: "title"}

更多应用请查阅Underscore API文档:

Underscore官网API:http://underscorejs.org/

Underscore中文版API:http://www.css88.com/doc/underscore/

发表评论

电子邮件地址不会被公开。 必填项已用*标注