Sighs at the core code of my jskit.

Source: Internet
Author: User

  Tutorial on helping customers (www.bkjia.com)I started to write my JSKit for a long time and was never satisfied with it. In the meantime, I downloaded a large number of frameworks to check that their architecture and design did not find a satisfactory version. In the end, I still made continuous improvements. In fact, I have been reducing the core code to my satisfaction. only two public methods are encapsulated in the version result. One is Import (Loading Code) the other is Run ).

Why are I not satisfied with so many excellent and powerful frameworks on the Internet? Because my requirements for the framework are actually very simple, I only need it to manage and organize my code. It's OK. I don't need to load a bunch of data in my implementation when implementing a simple function. I hate this kind of waste of code that cannot be used in a function process, so I haven't found anything that I like for a long time.

As a program that has not been started for a long time, I have been pursuing simple logic code. So I am not familiar with some waste of time. I cannot understand some complicated logic for a long time, and I am writing it in the end. Looking back on the coding path over the years, we sadly found that we can only be at the entry level in our life and cannot reach a higher level of experts (teachers), because we have learned a lot, the energy is dispersed. If there is an afterlife, I think I will fight for a dark path.

In addition, let's talk about the following code. Because there are few codes, there is no comment to write. Just talk about the ideas.

Import method: load a class, which can be a very simple class or a framework. The idea of my organization management code is mainly implemented through the import method. For example, I created a com. the imcjj sub-folder contains a Browser. the js file passes JK. import ("com. imcjj/Browser ") import the Browser. the js File Code "JK" is an example of JSKit.

Reference content is as follows:
/**
* CJJ's JavaScript Kit
*
* @ Type: class
* @ Version: v1.0 build 20090826
* @ Memo: none
*-----------------------------------
* Copyright (c) cjj rusty_sun [AT] hotmail.com
*/
Function JSKit (path ){
If (! Path) {return null };
/* Private member variable */
Var _ runTimer = null, _ importManage = [], _ path = path;
/* Public method */
JSKit. prototype. Import = function (file ){
Var script;
Var jskit = this;
If (! _ ImportManage [file]) {_ importManage [file] = "loading "};
If (_ importManage [file]! = "Loaded "){
Script = document. createElement ("script ");
Script. src = _ path + file + ". js ";
Script. onreadystatechange = function (){
If (this. readyState = 'complete '){
Var lastClass = file. substring (file. lastIndexOf ("/") + 1, file. length );
If (lastClass = "Import" | lastClass = "Run") {return };
Jskit [lastClass] = eval (lastClass );
_ ImportManage [file] = "loaded ";
Script. onreadystatechange = null;
Return
}
};
Script. onload = function (){
Var lastClass = file. substring (file. lastIndexOf ("/") + 1, file. length );
If (lastClass = "Import" | lastClass = "Run") {return };
Jskit [lastClass] = eval (lastClass );
_ ImportManage [file] = "loaded ";
Script. onload = null;
};
Document. getElementsByTagName ("head") [0]. appendChild (script );
}
};/* End of Import */
JSKit. prototype. Run = function (code ){
Var loaded = true;
Var jskit = this;
For (importFile in _ importManage ){
If (_ importManage [importFile] = "loading") {loaded = false; break}
};
If (! Loaded) {_ runTimer = setTimeout (function () {jskit. Run (code)}, 500); return };
If (_ runTimer) {clearTimeout (_ runTimer )};
If (typeof (code) = "function") {code ()}
};/* End of "Run "*/
}

  Call Demo:

Reference content is as follows:
Var JK = new JSKit ("/jskit. src /");
If (JK ){
JK. Import ("com. imcjj/FormValidate ");
JK. Import ("com. imcjj/Browser ");
JK. Run (function (){
Var browser = new JK. Browser ();
Alert (browser. name + "\ n" + browser. agent );
Var oFormValidate = new JK ["FormValidate"] ("frmDemo ");
Var oObj = oFormValidate. _ form;
If (! OObj) {alert ("no form is found and form verification cannot be performed"); document. close ();}
Var oTip = new Tip ();
OTip. _ struct = "<strong >{# text #}</strong> ";
OTip. _ inertDirection = 0;
OTip. add ("tip_Name", "tip_bottom", "custom message", oObj. elements ["Name"]. parentNode, "SPAN", "<strong >{# text #}</strong> ");
// Add an external verification function
// OFormValidate. addValidate ("isDate", fnCheckDate, "f", "is the date correct? ");
// Parameter: verification method. The ID attribute of the form element to be verified plus the * sign indicates that this item is required.
OFormValidate. addRule ("isNotNull", "* Card ");
// OFormValidate. addRule ("isDate", "* Birthday ");
// Call custom verification rules
// AJAX verification the last one is the list of AJAX verification function parameters: 'getname. asp
OFormValidate. addRule ("isExists", "Name", ["'getname. asp '"], oTip );
OFormValidate. addRule ("isLen", "* Password", [4, 8]);
OFormValidate. addRule ("isSame", "* Password1", ["'password2 '"]);
OFormValidate. listen ();
});
}

Related Article

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.