Top

组件开发-命名规范

组件命名规范

=

项目案例  获取表格中的数据,返回JSON个对象

实现方式一写法:hct-*命名

 1     /**
 2      * 获取表格中的数据:input:text radio checkbox password
 3      * 通过名称获取,并会获取tr上的Id
 4      * 
 5      * @method getTableVal
 6      * @param  {Object} $tbody table的query对象
 7      * @return {Object}        返回JSON
 8      */
 9     this.getTableVal = function($tbody, idName) {
10         var res = false;
11 
12         if (!!$tbody && $tbody.length) {
13             res = [];
14             var name, value;    
15             idName = idName || 'id';
16 
17             $tbody.children('tr').each(function() {
18                 var $tr = $(this), val = {id: $tr.data(idName)};
19                 $tr.find('input,select').each(function() {
20                     var $that = $(this);
21                     name = $that.prop('name');
22                     if (!!name) {
23                         if ($that.is('[type=checkbox],[type=radio]')) {
24                             value = $that.is(':checked')?1:0;
25                         } else {
26                             value = $that.val();
27                         }
28                         val[name] = value;
29                     }
30                 });
31                 res.push(val);
32             });
33         }
34 
35         return res;
36     };

实现方式二写法:jq-*命名

/**
 * 获取表格中的数据:input:text radio checkbox password
 * 通过名称获取,并会获取tr上的Id
 * 
 * @class getTableVal
 * @constructor
 */
+function ($) {
    'use strict';
    /**
     * test doc
     * @method idName
     * @param  {Object} $tbody table的query对象
     * @return {Object}        返回JSON
     */
    var getTableVal = function(idName){
        var res = false,
            $tbody = this;

        if (!!$tbody && $tbody.length) {
            res = [];
            var name, value;    
            idName = idName || 'id';

            $tbody.children('tr').each(function() {
                var $tr = $(this), val = {id: $tr.data(idName)};
                $tr.find('input,select').each(function() {
                    var $that = $(this);
                    name = $that.prop('name');
                    if (!!name) {
                        if ($that.is('[type=checkbox],[type=radio]')) {
                            value = $that.is(':checked')?1:0;
                        } else {
                            value = $that.val();
                        }
                        val[name] = value;
                    }
                });
                res.push(val);
            });
        }

        return res;
    };
    $.fn.getTableVal = getTableVal; //将getTableVal方法挂到jQuery对象上
    

}(jQuery);

 修改原生插件及扩展pt-*

//修改原生方法
String.prototype.trim = function () {
    var str = this;
    if (!!str) {
        str = str.replace(/(^\s*)|(\s*$)/g, "");
    }
    return str;
}

//扩展
$.fn.extend({
    trim : function (str) {
        if (!!str) {
            str = str.replace(/(^\s*)|(\s*$)/g, "");
        }
        return str;
    }
});

 

posted @ 2016-07-11 22:18  Avenstar  阅读(1094)  评论(0)    收藏  举报