ben lowery

Dojo’s little gems

Apr 26 2008

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" });

Related posts:

  1. Monday the 4th
  2. Using post-build events to make your own app.config
  3. Children of the world, all in NYC
  4. Monday the 7th
  5. The Page Event Life Cycle

One Response to “Dojo’s little gems”

  1. Rob Coup says:

    An improvement for the last one …

    config = dojo.delegate(defaults, {color:”yellow”});

    Rob :)

Leave a Reply