Wednesday, January 11th, 2006
Dojo Tips: Cookies, and a nice Curry
Dojo continues to have nice wrappers that make it easy for you to do real things.
A couple of topics came up this week such as cookies, and a new curry mechanism:
Dojo Cookies
Dojo has helper cookie methods wrapped up in the dojo.io.cookie package (src).
Example usage:
dojo.io.cookie.set(”mycookie”, “my value”, 30); // set ‘mycookie’ to ‘my value’, expires in 30 days
alert(dojo.io.cookie.get(”mycookie”)); // should alert “my value”
Cache Busting
If you use the dojo.require(..) mechanism, you may need to push through the browser cache that helps out GET requests (and hurts in this case).
Simply use the djConfig.cacheBust value:
djConfig.cacheBust = new Date().valueOf().toString(); // random value. bad in all but dev
djConfig.cacheBust = “version-0.2.1″; // be explicit
dojo.require(”dojo.string”); // will get string.js?version-0.2.1
Can I have a curry with those cookies?
Alex Russell added a curry method:
var curried = dojo.lang.curry(namespace, funcName /* args … */);
The returned function executes in the context of namespace with whatever arguments are passed past that filled in as the expected arguments. So if the function defined is:
function foo(bar, baz, xyzzy, thud){ … }
we can “fill in” some arguments using curry(), like this:
var foo2 = dojo.lang.curry(null, "foo", "bar value", "baz value");And now foo2 is a function which expects 2 arguments instead of 4.
Calling it with fewer than 2 arguments will return a similarly “filled in” function that expects the remaining number of methods:
var foo3 = foo2(”xyzzy value”);
// foo3 now expects 1 value
When foo3 is now called with one or more arguments, it will “unroll” the chain and call foo() with the accumulated arguments.












I have to say I’m continually impressed with the richness of Dojo!
Dojo includes loads of well-chosen bits of sweetness from other programming environments that add up to a very impressive kit.
Function currying (also in ML, Groovy, …) is one of those simple/powerful concepts that takes you a long way for a small fare.
The pub/sub model for event notifications is another Dojo winner, as well as the stuff borrowed from AOP (around advice, after advice, etc).
AOP in Javascript… who’da thunk it?
Test your code before you publish it…
will run the function “foo” rather than return a reference.var foo2 = dojo.lang.curry(null, "foo", "bar value", "baz value");
I have tried your cookie code, but I got this error:
dojo.io.cookie has no propertiesFortunately, I found a solution. Added this line and it works.
<script type="text/javascript" src="/scripts/dojo/src/io/cookies.js"></script>Well, you should use the Dojo package loading mechanism,
dojo.require("dojo.io.cookie").