Note: The seajs version used in this article is 2.1.1
1. Encapsulate Jquery into seajs module
//Put jquery code here and just put the jquery version you like in
return $.noConflict();
});
Calling method:
In this way, you can use jquery as before
var $ = require('./js/jquery');
// $(document).ready(function () {
// $("tr").wyhinterlaced({ "odd": "red", "even": "blue" });
// $("tr").wyhhover();
// })
});
2. Encapsulate jquery classes into seajs module
var $ = require("../js/jquery");
var weekday = new Array(7)
weekday[0] = "Monday";
weekday[1] = "Tuesday";
weekday[2] = "Wednesday";
weekday[3] = "Thursday";
weekday[4] = "Friday";
weekday[5] = "Saturday";
weekday[6] = "Sunday";
function GetType(arg) {
var today = new Date();
var year = ();
var month = () + 1;
var td = ();
var d = weekday[() - 1];
var h = ();
var m = ();
var s = ();
switch (arg) {
case 1: //2013-09-09 09:31:56
return year + "-" + month + "-" + td + " " + h + ":" + m + ":" + s; break;
case 2: //2013-09-09 (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-09-2013 Monday 09:31:56
return month + "-" + td + "-" + year + " (" + d + ") " + h + ":" + m + ":" + s; break;
case 5: //September 9, 2013 09:31:56
return year + "year" + month + "month" + td + "day " + h + "hour" + m + "minute" + s + "seconds"; break;
case 6: //Monday, September 9, 2013 09:31:56
return year + "year" + month + "month" + td + "day (" + d + ") " + h + "hour" + m + "minute" + s + "seconds"; break;
}
};
/*******************************************************
/*Function name: GetTime
/* Parameters: numerical values (including shaping floating point types are OK. Floating point types will be rounded. If they are not numbers,
The function will be returned in the default time format! Time styles have 15 [1-15 are valid time styles
If it exceeds or is less than, the default style will be used in Style 1)
/*Function Get the current system time Customizable format
*******************************************************/
function GetTime(arg) {
if (!isNaN(arg)) {
var num = (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 (/week/, ""); break;
case 8: str = GetType(1); return (/-/, "/").replace(/-/, "/"); break;
case 9: str = GetType(2); return (/-/, "/").replace(/-/, "/");
case 10: str = GetType(2); str2 = (/-/, "/").replace(/-/, "/"); return (/week/, ""); break;
case 11: str = GetType(4); return (/week/, ""); break;
case 12: str = GetType(3); return (/-/, "/").replace(/-/, "/"); break;
case 13: str = GetType(4); return (/-/, "/").replace(/-/, "/");
case 14: str = GetType(4); str2 = (/-/, "/").replace(/-/, "/"); return (/week/, ""); break;
case 15: str = GetType(6); return (/week/, "");
default: return GetType(1); break;
}
}
}
else {
return GetType(1);
}
};
/* Get the current number of years of the system*/
function GetYear() {
var today = new Date();
return ();
};
/*Get the current number of months of the system*/
function GetMonth() {
var today = new Date();
return () + 1; ;
};
/*Get the current number of days of the system*/
function GetDay() {
var today = new Date();
return (); ;
};
/*Get the current hour of the system*/
function GetHours() {
var today = new Date();
return ();
};
/*Get the current minute of the system*/
function GetMinute() {
var today = new Date();
return ();
};
/*Get the current number of seconds of the system*/
function GetSecond() {
var today = new Date();
return ();
};
/************************************************************
*Function name: TimeSubMillisecond
* Parameters: endtime (end time) starttime (start time)
*Function: To obtain the difference between the milliseconds of two times, one parameter must be written. The second parameter (start time) can
*Default is the current system time if you don't write
************************************************************/
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
* Parameters: endtime (end time) starttime (start time)
*Function: To obtain the difference between two times, one parameter must be written. The second parameter (start time) can
*Default is the current system time if you don't write
************************************************************/
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 = (seconds / 60);
var hours = (minutes / 60);
var days = (hours / 24);
var CDay = days;
var CHour = hours % 24;
var CMinute = minutes % 60;
var CSecond = (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 + "second";
}
return str;
}
};
= GetTime;
= GetYear;
= GetMonth;
= GetDay;
= GetHours;
= GetMinute;
= GetSecond;
= TimeSubMillisecond;
= TimeSubNormal;
})
Calling method:
var $ = require('./js/jquery');
var a=require('./js/time');
alert((3));
});
3. Encapsulate the jquery plug-in into a seajs module
Below is an example of encapsulating a jquery plug-in into a module
return function (jquery) {
(function ($) {
//Highlight the current line
$. = function (options) {//options This often uses this to indicate that there are many parameters.
var defaultVal = {
BackColor: '#ccc',
};
var obj = $.extend(defaultVal, options);
return (function () {
var tabObject = $(this); //Get the current object
var oldBgColor = ("background-color"); //Get the background color of the current object
(//Define a hover method.
function (){("background-color", );},
function () {("background-color", oldBgColor);});
});
}
//Make the odd and even rows of different colors
$. = 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 (function () {
var tabObject = $(this); //Get the current object
if(()%2==0)
{
("background-color", );
}else
{
("background-color", );
}
});
}
})(jquery);
}
})
Calling method:
Calling plugins using shared
var $ = require('./js/jquery');
require('./js/jquery_tr')($);//Share to jquery
$(document).ready(function () {
$("tr").wyhinterlaced({ "odd": "red", "even": "blue" });
$("tr").wyhhover();
})
});