My dojo top ten (part 1 array processing)
I’ve been using the dojo toolkit a lot lately and I’ve come to really like it. Except for the documentation. The Book of Dojo is pretty good (though not comprehensive enough) but the api documentation sucks. There are a few resources on the web that are helpful but we’ve been relying heavily on two books: Dojo the Definitive Guide and Mastering Dojo.
So this is the first in a series of posts on my top ten dojo functions.
Dojo’s array processing functions basically make Javascript 1.6 array functions available in all browsers. These are extremely powerful functions that let you filter, transform, and query arrays.
I’m going to skip indexOf, lastIndexOf, some, every, and forEach (which are great, go look them up) and focus on map, and filter.
I’ve used dojo.map alot lately to transform the elements in an array. A somewhat contrived example: You get some json data back from a web service that is an array of objects. You’ve got to pass that array to another function but it expects each element of the array to have a name property instead of the title property they’ve got. Just call
var newAry =
dojo.map(ary, function(x){ x.name = x.title; return x; });
and voila, you’ve got a new array, and each element now has a name property that is the same as the title property.
I’ve also been using dojo.filter to filter arrays. Say you’ve got an array of objects and you want to work with a subset of them. You can use dojo.filter to filter the array. Just pass it the array and a function that evaluates each element and returns true if it should be included in the filtered array and false if not. Using the above example, lets say we want to return all elements in the array whose title starts with ‘A’. Just do this:
var newAry =
dojo.filter(ary, function(x){ return x.title.indexOf('A') == 0; });
As I said, these are extremely powerful functions and I’ve only scratched the surface in this post.
My first Virtual Earth post
So I’ve got a page with a virtual earth map into which I load a bunch of shapes. When the user mouses over a shape, I wanted the fill color to change and the infobox to popup. My initial plan was to use onmouseover and onmouseout but I found that when you mouse over the infobox, the onmouseout event fires. So instead of using the onmouseover and onmouseout events, I handled everything in onmousemove and keep track of my ‘current’ shape (_currentWatershed). I also check to see whether the shape the mouse is over is the same as the _currentWatershed and if so, I don’t do anything.
function mouseMoveHandler(e)
{
if (e.elementID == null)
{ //if we moused off all features
//and there was a _currentWatershed set
if (_currentWatershed != null)
{
_currentWatershed.SetFillColor(_watershedsFillColor);
_map.HideInfoBox(_currentWatershed);
_currentWatershed = null;
}
}
else
{
var shape = _map.GetShapeByID(e.elementID);
if (shape != null &&
shape.GetShapeLayer() == _watershedsLayer &&
shape != _currentWatershed)
{
if (_currentWatershed != null)
_currentWatershed.SetFillColor(_watershedsFillColor);
_currentWatershed = shape;
_currentWatershed.SetFillColor(_watershedsHoverFillColor);
_map.HideInfoBox();
//shows the infobox at the mouse position when this event fires
_map.ShowInfoBox(_currentWatershed, new VEPixel(e.mapX, e.mapY));
}
}
}
Accessing ASP.NET Web Services with Dojo
A while back, Dave wrote a post on this subject. Last week, I decided to pick up where he left off.
I’ve got some web services that take parameters so I decided to try to figure out how to pass the parameters up using dojo.xhrGet. You’ll have to check out Dave’s post for the fundamentals but here’s what I did. I created a criteria object with the parameters that I needed to pass to the webservice (the vals object was returned by a call to my dojo dialog’s getValues method):
var criteria = { “projectName”:’”‘ + vals.projectName + ‘”‘, “bufferDistance”:vals.bufferDistance };
then I added the criteria to my dojo.xhrGet (note the ‘content: criteria’ part):
dojo.xhrGet({
url: 'Services/FuelsSearchService.asmx/GetByProject',
handleAs: 'json',
contentType: "application/json; charset=utf-8",
content: criteria,
load: handleFuelsResults,
error: callbackError,
timeout: 10000
});
And it works!
Well, not right away. See those funky extra quotes around the projectName value? Well it wouldn’t work until I added those. Even though you and I know that vals.projectName is a string, dojo knows it’s a string, and javascript knows it’s a string, Microsoft’s javascript deserialization stuff apparently doesn’t know that. I kept getting “Error: bad http response code:500″. But when I actually looked at the response in Firebug, it said, “Invalid JSON primitive: Cities.” Cities is the value of vals.projectName. So I added the extra quotes and voila, it works. But it’s ugly. Anybody know another way?
Hello world!
So, this is my blog. I’ll probably mostly write about GIS software development here. If you want pictures of my kids and such, check out my other blog.
