Wednesday, January 11th, 2006

Dojo Tips: Cookies, and a nice Curry

Category: Articles, Dojo, JavaScript, Toolkit

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 package (src).

Example usage:"mycookie", "my value", 30); // set 'mycookie' to 'my value', expires in 30 days
alert("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.

Posted by Dion Almaer at 1:10 am

3.4 rating from 31 votes


Comments feed TrackBack URI

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?

Comment by Ken Pelletier — January 13, 2006

Test your code before you publish it…
var foo2 = dojo.lang.curry(null, "foo", "bar value", "baz value");
will run the function “foo” rather than return a reference.

Comment by Ivan — September 11, 2006

I have tried your cookie code, but I got this error: has no properties

Fortunately, I found a solution. Added this line and it works.
<script type="text/javascript" src="/scripts/dojo/src/io/cookies.js"></script>

Comment by Top Ten Investment Blog — August 16, 2007

Well, you should use the Dojo package loading mechanism,

Comment by Julian — October 9, 2007

Leave a comment

You must be logged in to post a comment.