SOS

Shane O'Sullivan's technical blog… really ties the room together

Archive for February, 2008

New Atom data store for Dojo

Posted by Shane O'Sullivan on February 20, 2008

I’ve just submitted a patch to the DojoX project for a dojo.data data store that can read Atom XML documents. It’s relatively simple for the moment, but is capable of taking an XML document that conforms to the Atom spec, and returning the entries using the dojo.data.Read API.

Update: this has now been committed to the DojoX project.

This arose out a desire to rewrite the page on my personal website that mirrors this blog. At time of writing, that has a strong dependency on PHP to query my blog for the Atom feed, parse the feed, and output it using a server side HTML template.

This is of course be easily achievable on the browser also, except for the fetching of the XML document from the remote website which the server would still have to do. Of course, if the Atom URL is on the same server as the page, even this dependency could be removed, and all the work, from the fetching, parsing and templating, could be done on the browser.

However, implementing this as a Dojo data store has many advantages, the primary one being that I get to play with integrating a Dojo data store with the very very cool Django templating language package in DojoX, dojox.dtl. Alex recently checked in a patch to dojox.dtl that enables the use of dojo.data stores directly in templates, so I can’t wait to get this checked in (once any issues Jared has with it are satisfied) and get going on my site!
Share this post:digg it|kick it|Email it|bookmark it|reddit|liveIt

Posted in Ajax, Alex Russell, Atom, Blogs, Dojo, dojo.data, dojox.data, Feeds, Javascript, json, open source, Technical | 2 Comments »

Functional programming with Dojo – Quick tips

Posted by Shane O'Sullivan on February 15, 2008

This post is a summary, and more, of a great blog post by Eugene Lazutkin on the insanely cool new Functional Programming features in the Dojo Ajax toolkit. It gives number of quick examples in the vein of : “How to I do (x) to a data structure of type (y)“. Have a read of Eugene’s post if you want to know the reasoning behind these features, and how they can be used to vastly simplify your own code.

Please feed free to post your own suggestions as comments and I will include them here. Note that some of the examples contain the variable df, this is short for dojox.lang.functional.

1. How do I add a number to every element of an array (creating a new array)?

var list= [10,20,30,50,25,5];
var newList= dojo.map(arr, "return arguments[0]+2");

2. How do I add a number to every element of an array without creating a new array?

var list= [10,20,30,50,25,5];
dojo.forEach(list, "array[index]+=2");

3. How do I sum all the values in an array?

var arr = [10,20,30,50,25,5];
var sum = df.reduce(arr, "+");

4. How do I sum all the values in a JSON array containing objects?

var json = [{value: 1, name:"foo"}, {value:25}, {value:12,name:"bar"}];
var sum = df.reduce(json, "{value:a.value+b.value}").value;

5. How do I check if a condition is true for all elements of an array?

var arr = [10,20,30,51,25,5];
var isTrue = dojo.every(arr, "return item % 5==0;");

6. How do I check if a condition is true for at least one element of an array?

var arr = [10,20,30,51,25,5];
var isTrue = dojo.some(arr, "return item % 5==0;");

7. How do I find the smallest element in an array?

var arr = [10,20,30,51,25,5];
var min = df.reduce(arr, "Math.min(a,b)");

8.How do I find the largest element in an array?

var arr = [10,20,30,51,25,5];
var min = df.reduce(arr, "Math.max(a,b)");

9. How to I calculate the percentage each element of an array makes up of the sum of all elements in the array?

var arr = [10,20,30,51,25,5];
var percents = df.map(arr, "/this", df.reduce(arr, "+"));

10. How do I generate a running total of an array?

var arr = [1,2,3,4,5];
var runningTotal = df.scanl1(arr, "+");
//Gives runningTotal=[1,3,6,10,15]

11. How do I create an array where each element is the multiplicand of all elements that came before it and itself?

var arr = [1,2,3,4,5];
var result = df.scanl1(arr, "*");
//Gives result=[1,2,6,24,120]

12. How do I combine multiple arrays into an equal number of arrays, but arranged by index?

var arraysByIndex = df.zip([1,2,3],[11,22,33],[111,222,333]);
//Gives arraysByIndex=[[1,11,111] , [2,22,222], [3,33,333]]

13. How do I search a dojo.data store for the existance of a named attribute?
//Assume a data store exists called 'store' and items have been retrieved
//and placed in an array called 'items'. We will search for an item that has
//an attribute 'country' equal to 'Ireland'
var found = df.some(items, "this.getValue(item, 'country') == 'Ireland'", store);

14. How do I filter an array to just the items that match a boolean condition?
var array = [22,4,66,100,55];
var over30 = dojo.filter(array, "return item > 30");
//Gives a new array [66,100,55]. The original array is not modified.

15. How do I find an element in an array?

var array = ["this","is","an","array","of","strings"];
var index = dojo.indexOf(array, "an");
//returns 2

16. How do I apply a Django Template to one or more nodes?

dojo.require("dojox.dtl.ext-dojo.NodeList");
var array = ["some", "sample", "template", "input"];
dojo.query(".someClass").dtl("templates/myTemplate.html", array);

17. How do I remove elements from an array without creating a new array?

var arr = [1,2,3,4,5,6,7,8,9,10];
// Removes all even numbers from the array
dojo.forEach(arr, "if(array[index] % 2 == 0){array.splice(index, 1);}");
// arr now contains [1,3,5,7,9]

Share this post:digg it|kick it|Email it|bookmark it|reddit|liveIt

Posted in Ajax, Dojo, dojo.data, dojox, Javascript, json, open source, Technical | 11 Comments »

 
Follow

Get every new post delivered to your Inbox.

Join 535 other followers

%d bloggers like this: