Coding Updates, Use of Eval and Other Mistakes

I’ve finally almost completed the way layer lists work.  The backend is completely done, and the front end is nearly finished also.  Sorting columns works (implementing MooTools), as does saving and editing a created layer.  It took me a while to figure out how to get the sortables working with the dynamically created layers, but it turned out to not be as big of a problem as I expected.  My problem was that the layers were being added after an event happened, so I simply added an event handler (solution).

I had been using eval() frequently to access properties of an object when I don’t know the property value until runtime.  For instance, I set the map.layers object property equal to the value of text in an input form.
<code>

//base problem

object.variable;

//my problem

map.layers[0].variable;

</code>

Normal I solved this by using eval.

<code>

eval(“map.layers[0].” + variable);

</code>

Not only is this messy, it’s slow.  After searching google to find a better way to do this, I found an excellent article at 24 ways to impress your friends.  The explanation there is more eloquent than mine, but following idea is the same.  It’s possible to use brackets [ ]’s to access properties that I would normally access via eval.  So, using brackets to get the property’s values, my code looks like

<code>

map.layers[0][variable];

</code>

Just make sure there’s no dot following the object – i.e. the correct syntax is

<code>

object[variable];

</code>

I registered the domain OLArchitect.com, once I feel that the application is a little more presentable I’ll be sure to upload it.  Until then, good luck to all fellow GSoC coders!

You can leave a response, or trackback from your own site.
Leave a Reply

Subscribe to RSS Feed My tweets