I haven’t used dojo query before today. It’s just a great tool, it’s so time saving… This is a great tool particularly for dojo form if you start playing with it.

Let’s look at how I used to get the data back of a form :

// Get the content of Title, Summary, WherePublished, PublicationDate, ShowProfile
Title = dijit.byId("Title").attr("value");
Summary = dijit.byId("Summary").attr("value");
WherePublished = dijit.byId("WherePublished").attr("value");
DatePublished = dijit.byId("DatePublished").attr("value");
//PublishProfile = dijit.byId("PublishProfile").attr("value");

// Get the content of the Editor
Editor = dijit.byId("Editor").attr("value");

And off we go to put this on a xhr query… Well the good new is that you can get all this (very) boring stuff in only one / two lines of code without the hassle of finding each field. Saves time no ?

Here it is :
data = [];
dojo.query(" textarea, .ArticleNewFormElement > input").forEach(function(node, index, arr){
if(node.id){
data[node.id] = dijit.byId(node.id).attr("value");
}
});

That’s it, you can send back data to your server. By simply placing your elements inside divs you control (ex. : MyClass) you’ll be able to get the content of all the elements of a form.

Damn I’ll never write these long fields getters anymore !!

Advertisements