How to encapsulate jQuery classes and plug-ins into seajs modules

Source: Internet
Author: User
Tags difference between two times

Note: The seajs version used in this article is 2.1.1.
1. encapsulate Jquery into a seajs Module
Copy codeThe Code is as follows: define (function (){

// Put the jquery code here and put your favorite jquery version in.


Return $. noConflict ();
});

Call method:
In this way, you can use jquery as before.
Copy codeThe Code is as follows: define (function (require, exports, module ){
Var $ = require ('./js/jquery ');

// $ (Document). ready (function (){
// $ ("Tr"). wyhinterlaced ({"odd": "red", "even": "blue "});
// $ ("Tr"). wyhhover ();
//})
});
Ii. encapsulate jquery classes into the seajs Module
Copy codeThe Code is as follows: define (function (require, exports, module ){

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


Var weekday = new Array (7)
Weekday [0] = "Monday ";
Weekday [1] = "Tuesday ";
Weekday [2] = "weekday ";
Weekday [3] = "Thursday ";
Weekday [4] = "Friday ";
Weekday [5] = "Saturday ";
Weekday [6] = "Sunday ";

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: // 09:31:56
Return year + "-" + month + "-" + td + "" + h + ":" + m + ":" + s; break;
Case 2: // (Monday) 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:31:56 on Monday
Return month + "-" + td + "-" + year + "(" + d + ")" + h + ":" + m + ":" + s; break;
Case 5: // September 09, 2013
Return year + "year" + month + "month" + td + "day" + h + "Hour" + m + "Minute" + s + "second"; break;
Case 6: // September 09, 2013
Return year + "year" + month + "month" + td + "Day (" + d + ") "+ h +" Hour "+ m +" Minute "+ s +" second "; break;
}
};

/*************************************** ****************
/* Function name: GetTime
/* Parameter: Numeric value (including integer floating-point type). The floating-point type is rounded down. If it is not a number,
The function will be returned in the default time format! There are 15 time styles [1-15 is a valid time style
If the value exceeds or is smaller than the value of the default style, the default style 1 is used)
/* Function to obtain the current custom system time format
**************************************** ***************/

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 (/week/, ""); 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 (/week/, ""); break;
Case 11: str = GetType (4); return str. replace (/week/, ""); 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 (/week/, ""); break;
Case 15: str = GetType (6); return str. replace (/week /,"");
Default: return GetType (1); break;
}
}
}
Else {
Return GetType (1 );
}
};


/* Obtain the current number of years of the system */
Function GetYear (){
Var today = new Date ();
Return today. getFullYear ();
};


/* Obtain the current number of months of the system */
Function GetMonth (){
Var today = new Date ();
Return today. getMonth () + 1 ;;
};
/* Obtain the current number of days of the system */
Function GetDay (){
Var today = new Date ();
Return today. getDate ();;
};
/* Obtain the current hour of the system */
Function GetHours (){
Var today = new Date ();
Return today. getHours ();
};
/* Obtain the current system minute */
Function GetMinute (){
Var today = new Date ();

Return today. getMinutes ();
};
/* Obtain the current number of seconds of the system */
Function GetSecond (){
Var today = new Date ();
Return today. getSeconds ();
};


/*************************************** *********************
* Function name: TimeSubMillisecond
* Parameter: endtime (End Time) starttime (Start Time)
* Function: gets the difference between two milliseconds. The second parameter (Start Time) must be specified.
* If this parameter is left blank, the current system time is used by default.
**************************************** ********************/
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;
}
};
/*************************************** *********************
* Function name: TimeSubNormal
* Parameter: endtime (End Time) starttime (Start Time)
* Function: gets the difference between two times. You must specify a parameter. The second parameter (Start Time) can be used.
* If this parameter is left blank, the current system time is used by default.
**************************************** ********************/

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 + "day ";
}
If (CHour> 0 ){
Str + = CHour + "Hour ";
}
If (CMinute> 0 ){
Str + = CMinute + "Minutes ";
}
If (CSecond> 0 ){
Str + = CSecond + "seconds ";
}
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;

})

Call method:
Copy codeThe Code is as follows: define (function (require, exports, module ){
Var $ = require ('./js/jquery ');
Var a = require ('./js/Time ');

Alert (a. GetTime (3 ));
});
3. encapsulate the jquery plug-in into the seajs Module
The following is an example of encapsulating a jquery plug-in into a module.
Copy codeThe Code is as follows: define (function (require, exports, moudles ){
Return function (jquery ){
(Function ($ ){
// Highlight the current row
$. Fn. wyhhover = function (options) {// options this is often used to indicate that there are many parameters.
Var defaultVal = {
BackColor: '# ccc ',
};

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

Return this. each (function (){
Var tabObject = $ (this); // get the current object
Var oldBgColor = tabObject.css ("background-color"); // obtain the background color of the current object
TabObject. hover (// defines a hover method.
Function (){tabObject.css ("background-color", obj. BackColor );},
Function () Export tabobject.css ("background-color", oldBgColor );});
});
}
// Make the parity line different colors
$. Fn. wyhinterlaced = function (options) {// options this is often used to indicate that there are many parameters.
Var defaultVal = {
Odd: '# DDEDFB ',
Even: '# fff ',
};

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

Return this. each (function (){
Var tabObject = $ (this); // get the current object
If (tabObject. index () % 2 = 0)
{
TabObject.css ("background-color", obj. odd );
} Else
{
TabObject.css ("background-color", obj. even );
}
});
}
}) (Jquery );
}

})

Call method:
Use the shared method to call the plug-in
Copy codeThe Code is as follows: define (function (require, exports, module ){
Var $ = require ('./js/jquery ');
Require ('./js/jquery_tr') ($); // share it with jquery

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.