I don't know if you are concerned about the various toolkit in Dojo while using various dojo spaces? Some toolkit may not be conspicuous, but it may be very helpful for our software products or projects. Today I will briefly talk about the two very useful toolkit in the dojo extension (dojox) control library-dojox. timing and dojox. string.
First, let's talk about timing. Maybe many of you have never heard of it, but I have to say that he has been there for a long time. Today, we will explain in two parts: dojox. timingdolater
In fact, we can see a rough idea through the name-"Do it later ". In fact, this is what he means. Let's look at this Code:
Initcustomerwidget: function (){
If (dojox. Timing. dolater (Dojo. isobject (main. customerinfo), this, 200) {return ;}
// Todo code
This. jsondata = Main. customerinfo;
... This. jsondata ......
............................
}
This is part of the program code in a real project: During page initialization, the "initcustomerwidget" method is called, and some areas and content on the page are initialized. However, the content needs to get "Main. customerinfo ", but" Main. the initial value of customerinfo is empty. Its value must be obtained after an asynchronous Ajax request is returned to the background. Of course, we do not know when the asynchronous request will return.
OK. The problem arises. How can this problem be solved? In this case, "dolater" is very useful. Added "If (dojox. timing. dolater (Dojo. isobject (main. customerinfo), this, 200) {return;} "after this code, the execution rule of" initcustomerwidget "is changed to the following:
Step 1. if "dojo. isobject (main. customerinfo) "is false," initcustomerwidget "will not be executed, but" initcustomerwidget "will be executed again after" 200 "milliseconds, and" dojo. isobject (main. customerinfo) "True or false: if false, return to" Step 2 ". If true, go to" Step 2"
Step 2. When "dojo. isobject (main. customerinfo)" is true, the following code is executed: "Todo code"
Sequence
I believe that you will often encounter the need to delay the execution of code on a certain end in web development. Generally, you will use the "setTimeout" method. However, if more than one piece of code needs to be delayed, in addition, when there is a latency in the code, we will find that if we still use "setTimeout", the readability of our code will be greatly reduced. Don't worry. The sequence tool of dojox is used to solve this problem. See the following code:
var seq = [ { func: [showMessage, window, "i am first"], pauseAfter: 1000 }, { func: [showMessage, window, "after 1000ms pause this should be seen"], pauseAfter: 2000 }, { func: [showMessage, window, "another 2000ms pause and 1000ms pause before"], pauseAfter: 1000 }, { func: [showMessage, window, "repeat 10 times and pause 100ms after"], repeat: 10, pauseAfter: 100 }, { func: returnWhenDone } // no array, just a function to call ]; seqObj = new dojox.timing.Sequence({}); seqObj.go(seq, function() { logMsg('done') });
As you can see, we only need to define a "seq" object to implement the complex latency function. "func" is used to input the function that requires delayed execution [function, scope (scope ), parameters (arguments)]. "Pauseafter" indicates the delay time, and "repeat" indicates the number of repeated executions. It can be seen that our code is not very clear. If we use "setTimeout" for writing, how hard it will be to read.
Dojox. String
Let's talk about the dojox string toolkit. As the name suggests, it must be an operator string. Many may remember the methods used to operate strings in Java, and many may be familiar with the methods used to operate strings in C ++. Here, the dojox. String package will bring you back to that era.
Builder
Let's take a look at the builder. Everyone should know that the new tool stringbuilder launched by Java almost replaces the stringbuffer we were familiar with before. The same is true here. See the following code:
var b = new dojox.string.Builder("foo"); b.append("foo"); b.appendArray([ "foo", "bar", "baz" ]); b.replace("foo", "baz"); b.replace("o", "a").replace("b", "f"); b.insert(10, "awesome"); b.insert(0, "foo").insert(3, "bar").insert(3, "zoo"); b.remove(2, 100); b.remove(5);
In fact, we should be able to guess what he is doing based on the name. He is almost simulating the stringbuilder method of Java. "Appendarray" connects all elements of the array with "", and on the string before append, the first parameter of insert is the position of insert, and the first parameter of remove is the position, the second parameter indicates the number of characters to delete an element.
Sprintf
Let's take a look at builder. People who have learned C are not familiar with this. dojo allows you to write C in the same way when developing web. Refer to the following code:
var sprintf = dojox.string.sprintf; sprintf("% d", 42) --- " 42" sprintf("% 15d", -42) --- " -42" sprintf("%+d", 42) --- "+42" sprintf("%05d", 42) --- "00042" sprintf("%-15d", 42) --- "42 " sprintf("%.2f", 42.8952) --- "42.90" sprintf("%.10f", 42.8952) --- "42.8952000000" sprintf("%06.2f", 42.8952) --- "042.90"
Are you back in college? The sprintf usage here is almost the same as the C language we learned earlier. "0" indicates that 0 is missing, and "-" indicates that spaces are filled in the right, "%. 10f "indicates that 10 digits are retained after the decimal point. If not, add 0 and so on ...... you can even copy the code that was originally written in C to run on the web page.
Let's take a look at the subsequent usage of sprintf in dojox:
sprintf("%1$s %2$s", "Hot", "Pocket") ---"Hot Pocket" sprintf("%1$.1f %2$s %3$ss", 12, "Hot", "Pocket") ---"12.0 Hot Pockets" sprintf("%(temperature)s %(crevace)s", { temperature: "Hot", crevace: "Pocket" })); ---"Hot Pocket" sprintf("%0*.*f", 3.14159265, 10, 2) ---"0000003.14" sprintf("%c", 36) ---'$'
It seems a bit complicated. Otherwise, the first two are believed to be clear, that is, simple order replacement. You can compare them with the third example. In the third example, we can see that sprintf can pass a JSON object as a real parameter, and its replacement rule is also very simple, that is, Simple Matching: (temperature) matches the key value "temperature ".
For "sprintf (" % 0 *. * F ", 3.14159265, 10, 2)", here the 2nd and 3rd parameters are used to specify the first two "*" numbers, so the translation is "sprintf (" % 010.2f ", 3.14159265)", so his output is "0000003.14 ".
Finally, for 36, the number of characters is '$', which is not surprising.
By the way, these methods in dojox. String have been optimized in performance, so we strongly recommend that you use them.
Dojox. Validate
Let's talk about the dojox validation toolkit. I don't know if you may worry too much about writing some JavaScript verification scripts, and you may not be able to cover all the situations? If so, the dojox verification tool kit will be your preferred choice. This toolkit contains almost all popular verification methods, such as email, IP, phone number, zip code, and so on. You no longer need to spend time and code for some verifications. You only need to add a line of code.
Validate
This is the basis of the validate toolkit, including many of the most basic verification tools:
tests.t(dojox.validate.isValidIsbn('0-596-00759-0')); tests.t(dojox.validate.isText(' x '));tests.f(dojox.validate.isIpAddress('024.17.155.040'));tests.f(dojox.validate.isUrl('http://.yahoo.com'));tests.f(dojox.validate.isEmailAddress('x@yahoo')); tests.t(dojox.validate.isInRange( '1', {min: 1, max: 100} ));tests.t(dojox.validate.us.isPhoneNumber('111/111-1111'));tests.f(dojox.validate.us.isSocialSecurityNumber('123-45 6789')); tests.t(dojox.validate.us.isZipCode('123456789')); tests.f(dojox.validate.ca.isPostalCode('1AZ 3F3'));
Let's introduce them one by one:
- "Isvalidisbn" determines whether it is an international standard number. The International Standard Book Number (ISBN.
- "Istext" indicates whether it is a qualified string. Note: if it is a space, it is unqualified.
- "Isipaddress", "isurl", "isemailaddress", and "isphonenumber" are simple and will not be described too much.
- "Issocialsecuritynumber", "iszipcode", "ispostalcode", and so on are social security numbers and zip codes respectively. Note that they all have national restrictions, and some are us. issocialsecuritynumber, some of which are Canadian "ca. ispostalcode ('1az 3f3 ')".
Creditcard
This is dedicated to credit card verification:
Tests. T (dojox. validate. isvalidluhn ('000000'); // test string input // CVV verification tests on the Visa card. T (dojox. validate. isvalidcvv (421, 'vi'); // The CVV of the American Express card verifies tests. T (dojox. validate. isvalidcvv ('20140901', 'ax'); tests. F (dojox. validate. isvalidcvv (43215, 'ax '); // tests verified by the Visa card number. T (dojox. validate. isvalidcreditcard ('000000', 'vi'); tests. T (dojox. validate. isvalidcreditcard ('000000', 'vi'); // American Express Card number verification tests. T (dojox. validate. isvalidcreditcard ('2017 378 2822 4631 0005 ', 'ax'); tests. T (dojox. validate. isvalidcreditcard ('2017-1111-1111-111111', 'ax '));
See the preceding notes, where
- "CVV", that is, the card verification value, is a three-or four-digit number generated by the card number, validity period, and service constraint code, it is generally written in the User-Defined data area of the two channels of the card tile, which is usually used for verification.
- The "isvalidcreditcard" interface is similar to the "isvalidcreditcardnumber" interface and can be used universally.
There are also some verifications dedicated to Brazil and other countries, which are not described here.
This article has been launched on the infoq Chinese site and is copyrighted. The original Article is "talking about some gadgets in dojox". If you need to reprint it, please attach this statement. Thank you.
Infoq Chinese site is an online independent community for mid-and high-end technical personnel ,. net, Ruby, SOA, agility, architecture and other fields to provide timely and in-depth information, high-end technology conferences such as qcon, offline technology exchange activities qclub, free mini book download such as architect, etc..