把jQuery的类、插件封装成seajs的模块的方法

一、把Jquery封装成seajs的模块

要将现有JS库如jQuery与SeaJS一起使用,只需根据SeaJS的的模块定义规则对现有库进行一个封装。例如,下面是对jQuery的封装方法:

define(function() {

//{{{jQuery原有代码开始
/*!  
 * jQuery JavaScript Library v1.6.1
 * http://jquery.com/
 *
 * Copyright 2011, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 * Copyright 2011, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: Thu May 12 15:04:36 2011 -0400
 */
//...
//}}}jQuery原有代码结束

return $.noConflict();
});

另外,其他插件封装运用的方式为

define(function(require, exports, module) {
    var $ = require('$');
      /*
       *中间为每个JS库的插件代码!
      */
});

调用方法:

这样引进就可以像以前一样使用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");
     /* 获取系统的当前年数*/
     function GetYear() {
        var today = new Date();
        return today.getFullYear();
    };
    /*******************************************************
    /*函数名: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);
        }
    };

    exports.GetYear = GetYear;
   exports.GetTime = GetTime;
});

调用方法:

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

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

可以查看static文件库

原文来自:http://www.jb51.net/article/47507.htm

 文章来自:http://www.jb51.net/article/48020.htm

 

 

 

posted @ 2015-12-04 16:58  chenguiya  阅读(227)  评论(0)    收藏  举报