Tuesday, April 22nd, 2008

Reminded of speaking your YAHOO.lang

Category: Examples, JavaScript, Yahoo!

Mitchell Amihod has a nice little post talking about the features in YAHOO.lang which is probably the least mentioned part of YUI.

He starts out with the type checking functions such as like isNull(), isUndefined(), isValue(); then he does for a trim().

Did you know that YUI had a mini template language in the form of substitute?

javascript

  1. // simple
  2. YAHOO.lang.substitute('Hello {world}', {'world':'earth'});
  3.  
  4. // process
  5. var processText = function(key, value, extraInfo) {
  6.   if(!YAHOO.lang.isNull(extraInfo)) {
  7.     return extraInfo;
  8.   }
  9.   return value.toUpperCase();
  10. };
  11. YAHOO.lang.substitute('Hello {world Venus, Jupiter}', {'world':'earth'}, processText);

No DHTML library would go without wrapping the setInterval/Timeout calls:

javascript

  1. var foo = {
  2.   count :0,
  3.   'method' : function(data) {
  4.     this.count++;
  5.     if(this.count == 10) {
  6.       timer.cancel();
  7.     }
  8.     console.log(this.count);
  9.   }
  10. }  
  11. var timer = YAHOO.lang.later(1000, foo, 'method', [{data:'bar', data2:'zeta'}], true);`

And then you have the need to merge. If you are used to Rails development you will get addicted to passing hashes around and using merge type operations to do your deed.

javascript

  1. var myAwesomelWidget = function(oConfigs) {
  2.   oConfigs = oConfigs || {};
  3.   var defaults = {
  4.     'awesomeness' : '11',
  5.     'shiny'       : 'high',
  6.     'sparkle'     : 'high'
  7.   }
  8.  
  9.   var combinedConfigs = YAHOO.lang.merge(defaults, oConfigs);
  10.   //Shiny is now set to low, everything else in combinedConfigs is set to the defaults
  11. };
  12. myAwesomelWidget({'shiny': 'low'});`

Posted by Dion Almaer at 5:43 am
Comment here

+++--
3.6 rating from 16 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.