jQueryのクラス・プラグインをseajsのモジュールにカプセル化する方法

10751 ワード

注意:本明細書で使用するseajsバージョンは2.1です.1一、Jqueryをseajsのモジュールにカプセル化する

   define(function () { 
   
  

   // jquery jquery


return $.noConflict();
});


呼び出し方法:これにより導入が以前のようにjqueryを使用できるようになります

   define(function (require, exports, module) { 
  
    var $ = require('./js/jquery');
 

   // $(document).ready(function () {
     //   $("tr").wyhinterlaced({ "odd": "red", "even": "blue" });
     //   $("tr").wyhhover();
   // })
});


二、jqueryのクラスをseajsモジュールにカプセル化する

   define(function (require, exports, module) { 
   
  

    var $ = require("../js/jquery");


    var weekday = new Array(7)
    weekday[0] = " ";
    weekday[1] = " ";
    weekday[2] = " ";
    weekday[3] = " ";
    weekday[4] = " ";
    weekday[5] = " ";
    weekday[6] = " ";

     function GetType(arg) {
        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth() + 1;
        var td = today.getDate();
        var d = weekday[today.getDay() - 1];
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        switch (arg) {
            case 1:  //2013-09-09 09:31:56
                return year + "-" + month + "-" + td + "  " + h + ":" + m + ":" + s; break;
            case 2:  //2013-09-09 ( ) 09:31:56
                return year + "-" + month + "-" + td + " (" + d + ") " + h + ":" + m + ":" + s; break;
            case 3:  //09-09-2013 09:31:56
                return month + "-" + td + "-" + year + "  " + h + ":" + m + ":" + s; break;
            case 4:  //09-09-2013 09:31:56
                return month + "-" + td + "-" + year + " (" + d + ") " + h + ":" + m + ":" + s; break;
            case 5:  //2013 09 09 09 31 56
                return year + " " + month + " " + td + "   " + h + " " + m + " " + s + " "; break;
            case 6:  //2013 09 09 09 31 56
                return year + " " + month + " " + td + "   (" + d + ")  " + h + " " + m + " " + s + " "; break;
        }
    };

    /*******************************************************
    /* :GetTime
    /* : ( , , ,
    ! 15【1-15
    1】 )
    /*  
    *******************************************************/

     function  GetTime(arg) {
        if (!isNaN(arg)) {
            var num = Math.round(arg);

            if (num < 7 && num > 0) {
                return GetType(num);
            }
            else {
                var str;
                var str2;
                switch (num) {
                    case 0: return GetType(1); break;
                    case 7: str = GetType(2); return str.replace(/ /, ""); break;
                    case 8: str = GetType(1); return str.replace(/-/, "/").replace(/-/, "/"); break;
                    case 9: str = GetType(2); return str.replace(/-/, "/").replace(/-/, "/");
                    case 10: str = GetType(2); str2 = str.replace(/-/, "/").replace(/-/, "/"); return str2.replace(/ /, ""); break;
                    case 11: str = GetType(4); return str.replace(/ /, ""); break;
                    case 12: str = GetType(3); return str.replace(/-/, "/").replace(/-/, "/"); break;
                    case 13: str = GetType(4); return str.replace(/-/, "/").replace(/-/, "/");
                    case 14: str = GetType(4); str2 = str.replace(/-/, "/").replace(/-/, "/"); return str2.replace(/ /, ""); break;
                    case 15: str = GetType(6); return str.replace(/ /, "");
                    default: return GetType(1); break;
                }
            }
        }
        else {
            return GetType(1);
        }
    };


    /* */
     function GetYear() {
        var today = new Date();
        return today.getFullYear();
    };


    /* */
      function GetMonth() {
        var today = new Date();
        return today.getMonth() + 1; ;
    };
    /* */
      function GetDay() {
        var today = new Date();
        return today.getDate(); ;
    };
    /* */
   function GetHours() {
        var today = new Date();
        return today.getHours();
    };
    /* */
     function GetMinute() {
        var today = new Date();

        return today.getMinutes();
    };
    /* */
     function GetSecond() {
        var today = new Date();
        return today.getSeconds();
    };


    /************************************************************
    * :TimeSubMillisecond
    * :endtime( ) starttime( )
    * : , ( )
    *
    ************************************************************/
    function  TimeSubMillisecond(endtime, starttime) {
        var end = new Date(endtime).getTime();
        if (!endtime) {
            return -1;
        }
        if (!starttime) {
            start = new Date().getTime();
        }
        else {
            start = new Date(starttime).getTime();
        }
        if (start > end) {
            return -1;
        }
        else {
            return end - start;
        }
    };
    /************************************************************
    * :TimeSubNormal
    * :endtime( ) starttime( )
    * : , ( )
    *
    ************************************************************/

      function  TimeSubNormal(endtime, starttime) {
        var end = new Date(endtime).getTime();
        var start;
        if (!starttime) {
            start = new Date().getTime();
        }
        else {
            start = new Date(starttime).getTime();
        }
        if (start > end) {
            return -1;
        }
        else {
            var alltime = end - start;
            var seconds = alltime / 1000;
            var minutes = Math.floor(seconds / 60);
            var hours = Math.floor(minutes / 60);
            var days = Math.floor(hours / 24);
            var CDay = days;
            var CHour = hours % 24;
            var CMinute = minutes % 60;
            var CSecond = Math.floor(seconds % 60);
            var str = "";
            if (CDay > 0) {
                str += CDay + " ";
            }
            if (CHour > 0) {
                str += CHour + " ";
            }
            if (CMinute > 0) {
                str += CMinute + " ";
            }
            if (CSecond > 0) {
                str += CSecond + " ";
            }
            return str;
        }
    };


    exports.GetTime = GetTime;
    exports.GetYear = GetYear;
    exports.GetMonth = GetMonth;
    exports.GetDay = GetDay;

    exports.GetHours = GetHours;
    exports.GetMinute = GetMinute;
    exports.GetSecond = GetSecond;
    exports.TimeSubMillisecond = TimeSubMillisecond;
    exports.TimeSubNormal = TimeSubNormal;

})


呼び出し方法:

   define(function (require, exports, module) { 
  
    var $ = require('./js/jquery');
    var a=require('./js/time');

    alert(a.GetTime(3));
});


三、jqueryプラグインのパッケージをseajsモジュールにする
次はjqueryのプラグインをモジュールにカプセル化した例です

   define(function (require, exports, moudles) { 
  
    return function (jquery) {
        (function ($) {
             //
  $.fn.wyhhover = function (options) {//options 。
        var defaultVal = {
         BackColor: '#ccc',
        };

         var obj = $.extend(defaultVal, options);

          return this.each(function () {
            var tabObject = $(this); //
            var oldBgColor = tabObject.css("background-color"); //
            tabObject.hover(// hover 。
            function (){tabObject.css("background-color", obj.BackColor);},
            function () {tabObject.css("background-color", oldBgColor);});
        });
        }
     //
        $.fn.wyhinterlaced = function (options) {//options 。
        var defaultVal = {
         odd: '#DDEDFB',
         even: '#fff',
        };

         var obj = $.extend(defaultVal, options);

          return this.each(function () {
            var tabObject = $(this); //
           if(tabObject.index()%2==0)
           {
              tabObject.css("background-color", obj.odd);
           }else
           {
             tabObject.css("background-color", obj.even);
           }
        });
        }
        })(jquery);
    }

})


呼び出し方法:共有方式でプラグインを呼び出す

   define(function (require, exports, module) { 
  
    var $ = require('./js/jquery');
    require('./js/jquery_tr')($);// jquery

    $(document).ready(function () {
        $("tr").wyhinterlaced({ "odd": "red", "even": "blue" });
        $("tr").wyhhover();
    })
});