PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

用类与原型写一个组件(三)--学习笔记_html/css_WEB-ITnose

原创
2016-06-21 09:00:57 1165浏览

上一篇中,我们为组件添加了“删除一条item”的功能,现在,再增加一个“增加一条item”的功能。

首先,在页面中增加一个按钮,用来触发添加功能(红框中的内容是对比上一篇新加的代码)。

在main.js中绑定按钮的单击事件,触发add函数。

再在smartList.js里写add函数。

 1   // 增加一条数据 2   SmartList.prototype.add = function (data) { 3  4     var isExist = false; 5  6     // 判断id是否已存在 7     $.each(this.list, function (i, val) { 8       if (val.id === data.id) { 9         isExist = true;10         return false; // 退出循环11       }12     });13 14     if (isExist) {15       console.log('SmartList: id ' + data.id + ' 已存在,无法添加');16       return;17     }18 19     this.list.push(data);20     this._genItem(data).appendTo(this.element);21   };

现在可以注意到,$.each(this.list, function (i, val) {...}这行代码重复了,在add里用到了,在remove里也用到了,所以,我们是否可以写一个函数来代替这段重复的代码呢?于是,写一个_getById方法如下:

 1   SmartList.prototype._getById = function (id, callback) { 2     var self = this; 3     var data = null; 4  5     $.each(this.list, function (i, val) { 6       if (val.id === id) { 7         data = val; 8         if (callback && typeof callback === 'function') 9           callback(i, data);10         return false;11       }12     });13 14     return data;15   };

现在可以用_getById方法来改写remove和add方法。改写前后对比如下:

完整代码如下:

index.html:

 1  2  3    4   Flexx 5    6    7    8    9   10   11   12 
13 14
15

Flexx Library

16
17
18
19
20
21 24
25
26
27
28
29
30
31
32
33
34 35 View Code

smartList.js:

  1 /**  2  * SmartList  3  *  4  * 数据源:id为必须项  5  */  6   7 + function (Flexx) {  8   'use strict';  9  10   // 定义一个SmartList类 11   function SmartList(selector, options) { 12  13     this.list = []; 14  15     // 覆盖默认属性 16     this.options = $.extend({ 17       // 默认属性 18       isDeletable: true, // 设置是否有行删除按钮 19       isEditable: true // 设置是否有行编辑按钮 20     }, options); 21  22     this.element = $(selector) 23       .addClass('panel-group xx-smartlist'); 24  25     console.log('----创建了一个SmartList对象----'); 26   } 27  28   // 更新列表 29   SmartList.prototype.setData = function (list) { 30     var self = this; 31     // 更新list数据 32     this.list = list; 33     // 清空dom容器 34     this.element.html(''); 35     // 渲染元素 36     $.each(this.list, function (i, data) { 37       self._genItem(data).appendTo(self.element); 38     }); 39  40     console.log('-> setData done', this.element); 41   }; 42  43   // 增加一条数据 44   SmartList.prototype.add = function (data) { 45     if(this._getById(data.id)){ 46       console.log('SmartList: id ' + data.id + ' 已存在,无法添加'); 47       return; 48     } 49     this.list.push(data); 50     this._genItem(data).appendTo(this.element);    51   }; 52  53   SmartList.prototype.remove = function (id) { 54     var self = this; 55  56     this._getById(id, function (i, data) { 57       self.list.splice(i, 1); 58       self.element.find('[data-id="' + id + '"]').remove(); 59     }); 60   }; 61  62   SmartList.prototype._getById = function (id, callback) { 63     var self = this; 64     var data = null; 65  66     $.each(this.list, function (i, val) { 67       if (val.id === id) { 68         data = val; 69         if (callback && typeof callback === 'function') 70           callback(i, data); 71         return false; 72       } 73     }); 74  75     return data; 76   }; 77  78   // 生成一个条目 79   SmartList.prototype._genItem = function (data) { 80     var self = this; 81     var heading = 'heading_' + data.id; 82     var collapse = 'collapse_' + data.id; 83     var html = [ 84       '
', 85 '', 95 '
', 96 '
', 97 data.content, 98 '
', 99 '
',100 '
'101 ].join('');102 103 var item = $(html);104 105 if (this.options.isDeletable) {106 item.find('[data-act="del"]').click(function () {107 self.remove(data.id);108 });109 }110 111 return item;112 };113 114 Flexx.SmartList = SmartList;115 116 }(window.xx = window.xx || {});View Code

main.js:

 1 + function () { 2   'use strict'; 3  4   // 创建一个用来测试的模拟数据 5   var members = [{ 6     id: 0, 7     title: 'Brandon', 8     content: 'hello' 9   }, {10     id: 1,11     title: 'Kim',12     content: 'hi'13   }, {14     id: 2,15     title: 'Bunny',16     content: 'hi'17   }, {18     id: 3,19     title: 'Lovelyun',20     content: 'hi'21   }];22 23   var groups = [{24     id: 1,25     title: 'Web Dev',26     content: 'hello again'27   }];28 29   $(function () {30     // 初始化一个SmartList的实例对象31     var memberList = new xx.SmartList('#memberList');32     var groupList = new xx.SmartList('#groupList', {33       isDeletable: false,34       isEditable: false35     });36     // 调用原型方法更新数据37     memberList.setData(members);38     groupList.setData(groups);39 40     $('#btnAdd').click(function () {41       memberList.add({42         id: 4,43         title: 'add',44         content: 'hi'45       });46     });47 48   });49 50 }();View Code

总结:

1、要尽量减少代码重复;

2、充分利用回调函数。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。