Categories
Etc

Dojo’s little gems

I really enjoyed Mitch’s article on YUI Lang’s little gems and thought it might be handy to see how to do the same things with Dojo. Here’s the translation:

Trim

var trimMe = "  trim me please  ";

//YUI 
var trimmed = YAHOO.lang.trim(trimMe);

//Dojo
// - exactly the same
trimmed = dojo.trim(trimMe);

Substitute

var processText = function(key, value) {  
  return value.toUpperCase();  
};  

//YUI
YAHOO.lang.substitute('Hello {world}', { world: 'earth'}, processText); 

// Dojo
// - Named placeholders are prefaced with a $
// - No support for extraValues from Mitch's example
dojo.require("dojo.string"); // not in base distribution
dojo.string.substitute('Hello ${world}', { world: "earth" }, processText);

Later

// YUI 
// - Docs indicate this return type void, which is a timer object?
YAHOO.lang.later(200, foo, "method", [{ data: "bar" }]);

// Dojo
// - No abstraction for timers in Dojo
// - Can use hitch to set calling scope for function
setTimeout(dojo.hitch(foo, "method", { data: "bar" }), 200);
//  or
setInterval(dojo.hitch(foo, "method", { data: "bar" }), 200);

Merge

var defaults = { color: "red", answer: 42 };
var config;

//YUI
config = YAHOO.lang.merge(defaults, { color: "blue" });

//Dojo
// - mixin takes a variable number of arguments
// - Values are written to the first object, hence passing {}
// - Later values have precendence
config = dojo.mixin({}, defaults, { color: "yellow" });
Advertisements

1 reply on “Dojo’s little gems”

Comments are closed.