Dynamic Variables Assignment?
In JS, I ran into a problem where I needed to create a shortcut string, and then turn that string’s value into a reference to a different object.
So for example, I create a string called ‘oliver’ under the variable shortcut like so:
var shortcut = 'oliver';
Then I have a bunch of other code in my framework, and to access any of it, I would write something like:
FRAMEWORK.doSomething();
I wanted the framework’s name space to be preserved (so that I could easily upgrade/update it without affecting any code), but I wanted to be able to use my shortcut handle to access the framework, such that writing the following would work as well:
oliver.doSomething();
This led me to the use of eval to run a js command in js itself to get the assignment/reference working properly like so:
eval(shortcut+' = FRAMEWORK');
This worked really well and met exactly what I was trying to do, but I’m always hesitant to use eval. I’ve heard terrible things about it’s security and performance (although most ajax is based on this function). I’ll look into it and update the post if I find anything, but for the mean while, this has helped me do what I was trying to. It allows me to write really general client side framework code that can then be extended easily into a new namespace without any conflicts. Very powerful stuff.
When I search for it on google, it didn’t come up right away. My search terms (hopefully writing this in the post will help someone else) were: dynamic javascript variable assignment.
ps. The picture is supposed to represent a ’shortcut’. not the best fit I don’t mind saying.
Update: window[name] = reference is the winner. I don’t know why it escaped me before. This should prevent the use of eval and will allow you to dynamically create variables in the window scope.
Professional CodeIgniter: Review (part 1)
So far, I’m done about half of Professional CodeIgniter by Thomas Myer. As you guessed, it’s about the php, MVC framework CodeIgniter. While I’ve spent a lot of time online at the documentation, tutorials, etc., I’d preferred to grab a book on it (the only one I could find that’s available) to get a more formal introduction.
In terms of how it’s written, I was very impressed by it very early on. The language is very clear and concise, and Myer does a great job of walking you through a very real world example. I was actually blown away by how well he can articulate fairly advanced concepts. However so far this was the highlight with some major let downs.
For me, I came into this expecting a book on how CodeIgniter worked, how it’s structured, it’s benefits, etc., but after having read half of it so far, it’s become much more like a basic php web development tutorial. There is a recurring tutorial that is themed throughout the book of how to setup a rudimentary e-commerce site. When the tutorial begins, some CodeIgniter specific code is presented, with outlines of the helper classes, organization of a request/mvc separation, etc., but very quickly, most things specific to the framework are forgotten, and CSS/JS/PHP becomes the focus.
Additionally, the focus seems to be targetted at a beginning. Perhaps that was the intention, but it was a real disappointment for me. This framework is exceptionally powerful, and while it’s seeming to operate much more like a library than a framework such as CakePHP, it has tons of usage, most of which doesn’t seem to come through in the first half. A lot of time is devoted to making ajax calls to a controller (understandably a notable topic, but not specific much to CI), pages and pages of CSS (at one point there was a 3 page stint of pure css code, which didn’t seem very relevant as it’s a PHP/CI book), and even JS.
I believe I understand the intention; Myer wants to take you from concept to delivery, and show you how everything in between fits together within the CI framework; however because of this, I’ve felt that the power of CI has been left behind for me to figure out in their documentation.
While CI does push convention over configuration, it does seem to come across as a very strong and powerful library rather than a framework (with some pre-req’s that push it into the latter category). This isn’t necessarily a bad thing, but since it’s the case, I would expect more of the library to be explained, and how it could be useful, rather than walking through a tutorial that is targeted much more at the new php developer delivering a larger project than they’re used to.
I’ll follow up with my second half of the book review shortly.