Passing Parameters to a Function on HTML Events

Let’s say I have some function foo that needs to be called when some HTML element is clicked:

function foo(a, b) {
    :
    :
    :
}

:

i = 100;
j = 200;
element.setAttribute("onclick", "foo(i, j)");

In the example above, the Javascript interperter will see the line element.setAttribute("onclick", "foo(i, j)"); and automatically figure out that i and j should be 100 and 200, respectively.

However, the same doesn’t work if you’re working with arrays indicies:

bar = new Array();
bar[0] = 100;
bar[1] = 200;
element.setAttribute("onclick", "foo(bar[0], bar[1])");

To work around it, I had to re-assign them to variables:

bar = new Array();
bar[0] = 100;
bar[1] = 200;

i = bar[0];
j = bar[1];
element.setAttribute("onclick", "foo(i, j)");

That definitely made me go…hunh??

Posted in Javascript, Tech | Tagged , , , , , , , | Leave a comment

Creating Django Apps

I’ve been doing a bit of work with the Django framework lately, and because I’m a total noob at it, Derek has been nice enough to hold my hand throughout the dev process.

Django relies on the MVC design pattern, so you’ll want to modularize your components by creating Django apps.  From the Django book:

if you’re using Django’s database layer (models), you must create a Django app. Models must live within apps. Thus, in order to start writing our models, we’ll need to create a new app.

The Django book is actually quite nice and useful, so if you’re new to Django, I recommend reading it.  I still haven’t gotten through the whole thing myself, but I’ve got Derek :P

The steps to setup a Django app is actually quite long, so I made a nice little cheatsheet to help me remember the steps, so I’d though I’d share:

  1. Open up the console and cd into your project folder.  To create the template files for your new app, you need to run python manage.py startapp [your app's name]
  2. Go into setup.py and add a new entry into the INSTALLED_APPS dictionary to include your new app, like this: '[Project Name].[App Name]',
  3. [OPTIONAL] Inside your new app’s folder, create a subdirectory to hold all your html templates.  By convention I called it templates
  4. In setup.py update the TEMPLATE_DIRS dictionary to include your html templates like this: CUR_DIR + '[App Name]/templates', where CUR_DIR = os.getcwd() + os.setp()
  5. If necessary, define the link to your application’s view in urls.py by modifying urlpatterns

You’ll still need to create the html templates and define views for your application, but that’s basically how to start a new app in a nutshell.

Let me know if I missed out any steps (or if something’s just plain wrong:S)

Posted in Django, Tech, python | Tagged , , , | Leave a comment

Adding a Project to SVN in Eclipse

This happens to me a lot. I’ll be working away on some test application in Eclipse, and after a bit of tinkering I’ll realize that the code is actually good enough to commit into the repo. So what do you do?

Well back when I didn’t know anything about Subclipse, I’d actually checkout the repo as a project, copy my code into the project, then do a SVN commit. Its convoluted, its annoying, and its stupid.

There’s actually an easier way to add projects to svn control, and I only had to bug Derek 1000x to find out!

  1. In the Package Explorer, right-click on the package, and go to Team -> Share Project…
  2. Work through the wizard that comes up, its pretty basic, select the kind of repo (CVS/SVN), select or add your repo, etc.
  3. When you’re done, the project will be linked with the repo.  You can now Commit/Team Synchronize project files to your heart’s content!
Posted in Eclipse, Tech | Tagged , , , , | Leave a comment

Selenium and Firefox

Derek and I have been working on a Django web app as a side project, and of course, we decided that we needed to do some testing on it.

Derek at set up some basic Selenium tests, but I was having some difficulty running them with Firefox.  The error message that I got said:

Could not start Selenium session: Failed to start browser session:
Error while launching browser

A few of the solutions out there suggested that I dump my temp folder, or even create a new profile and tell Selenium to use it.  Unfortunately, none of those solutions work.

It turns out that Selenium RC (I’m running 1.01)  doesn’t play well with Firefox 3.6.  I had to uninstall Firefox 3.6 and re-install Firefox 3.5 to get it running.

Sigh.

Posted in Tech | Tagged , , , , | Leave a comment

Seriously?

New blog time!  Less techno jargon and more opinion! http://life.pioverpi.net/

Posted in News | Leave a comment