Revelations of the evening

I spent most of this evening banging my head against the wall, shaving one yak after another, trying to make headway on JSDBI. A full fledged yak shaving expedition.

How to get Venkman to run on Firefox on Gentoo

First off, I spent a fair amount of time trying to get Venkman, the javascript debugger for Firefox, to work on my Gentoo box. I kept getting the error:

An exception occurred while initializing, please file a bug.
BadMojo 4: JS Debugger Service is not installed. @
line 122 (initDebugger)

Turns out I needed to recompile Firefox with the mozdevelop flag set, which sets –enable-jsd in the configure for firefox. If only the error message was more helpful. There’s a growing thread on bug 288777 about it.

How Javascript Object.prototype works

Javascript only barely supports inhertance, by way of the Object.prototype property. If an object gets a method called on it that it doesn’t have, it looks at it’s own prototype. If it doesn’t find it there, then it looks at the prototype’s prototype, and so on recursively, until it either bottoms out, or finds a method to satisfy it. There’s a really good writeup on javascript inheritance by Kevin Lindsey.

How to use prototype.js to get both object method inheritance and class method inheritance

Do the following:

ChildClass = Class.create();
ChildClass.extend(ParentClass);
ChildClass.prototype = (new ParentClass()).extend( {
initialize: function (){
}
});

Then both methods and properties in the prototype of ParentClass get carried over, as well as things like ParentClass.myStaticMethod.

Some sample code for JSDBI

I’ve decided to temporarily name this Class::DBI meets ajax idea JSDBI, for lack of a better name (subject to change when I find something better). I’ve also sat down and translated the synposis example for Class::DBI into an equivalent JSDBI version. Code is after the jump.

Also, crw pointed me at json and json-rpc. These seem to be in the same ballpark, but json is just a data format (like XML), albeit, an easier format to parse than XML, and json-rpc looks purely procedural, rather than object based like Class::DBI, so it looks like there’s still room for improvement in this area. However, both could possibly be implemented as the transport portion of this overall solution, instead of using xml/rest.


// ***** Setup ******
var Music.DBI = Class.create();
Music.DBI.prototype = (new JSDBI()).extend( {
});

var Music.Artist = Class.create();
Music.Artist.prototype = (new Music.DBI()).extend( {
});// basic class setup
Music.Artist.table(’artist’);
Music.Artist.columns([’artistid’, ‘name’]);
Music.Artist.has_many(’cds’, ‘Music::CD’});

var Music.CD = Class.create();
Music.CD.prototype = (new Music.DBI()).extend( {
});

// basic class setup
Music.CD.table(’cd’);
Music.CD.columns([’cdid’, ‘artist’, ‘title’]);

// relationships ?
Music.CD.has_many(’tracks’, ‘Music::Track’});
Music.CD.has_a(’artist’, ‘Music::Artist’});

var Music.Track = Class.create();
Music.Track.prototype = (new Music.DBI()).extend( {
});
// basic class setup
Music.Track.table(’track’);
Music.Track.columns([’trackid’, ‘cd’, ‘position’, ‘title’]);

// ***** Usage ******

// create
var artist = Music.Artist.insert({artistid: 1, name: ‘U2′});
var cd = artist.add_to_cds({cdid: 1, title: ‘October’, year: 1980});

// update
cd.year(1981);
cd.update();

for (var i=0; i
var track = (cd.tracks)[i];
document.write(track.position+’ ‘+track.title);
}

cd = Music::CD.retrieve(1);

cd.delete();

Class::DBI + Ajax = Javascript OO-Ajax mapper?

I’m working on a new application that uses AJAX heavily on the front end, and catalyst/Class::DBI heavily on the backend. All in all, it seems to be working out relatively well. However, I realized the other day that it feels like my javascript frontend feels like it’s lacking sufficient abstraction. I find I’m writing a lot of code to make ajax calls, read in the results, and stick them in a javascript object

Instead, what I’d really like is something like Class::DBI for AJAX/javascript. I could give it a REST interface, and it would build me appropriate OO classes, for each object represented in the REST interface. I could then call CRUD methods on the javascript object, and have it perform the appropriate actions via the REST interface. I imagine this working very similar to the way Class::DBI currenly works:

  • create - creates a new javascript object with the data I pass it, and tells the REST interface to create a matching object. Pulls an automatically assigned key back from REST and sets it in the javascript object, if necessary
  • retrieve - retrieves an object by its primary key id
  • accessors - these are lightweight and provide set/get functionality all in one function, and mark field dirty so they can get updated during an update call
  • update - syncs the javascript object to the database, syncing whatever dirty fields there are. Autosync would also be an option that you could set, whereby syncs would happen as soon as a field was set. One thing to think about here is how to handle two different clients with copies of the same object in javascript at the same time. Should this OO mapper automatically handle syncing a change back out to the clients? Ie, if browser A has a copy of object b, and browser B also has a copy of object b, does an update from browser A to object b get pushed out to browser B? This could be pretty cool, but could be more complicated to build and use.
  • delete - deletes the object in REST

Note that search isn’t in the above list. Not that I don’t think search isn’t important, but it seems less clear how to provide that in a generic way.

One thing that REST lacks is introspection. I gather that this is one of the features that SOAP provides, however (the whole discoverability thing). So you couldn’t do the equivalent of Class::DBI::Loader without extra syntactic sugar on the server end. However, I’m not sure that’s what you’d want anyway. Because AJAX calls are relatively expensive, it seems like you’d want to know ahead of time (ie, compiled into the javascript) what objects the interface supported, and what fields those objects had.

Also of interest here is the issue of access control. In Class::DBI, for the most part, there isn’t really a concept of permissions. The database is expected to handle it, or there’s simply the expectation that anyone who has access to the database via Class::DBI is trusted. However, when you push access out to the client side, all of a sudden access controls become a huge issue. However, I’m not sure it’s as big a deal as it initially sounds (though I could be wrong, and feel free to point out where). If you think of REST as being your database, then again, the database is in charge of access control. So build a relatively generic/uniform REST interface, and simply throw errors (which the javascript should be able to parse) if the client tries to do something it’s not allowed to do.

It seems like this could all be accomplised through a clever catalyst helper. The helper would be responsible for taking in a Class::DBI object, and spitting out a REST controller (and appropriate views, if needed), as well as a controller which could be called to get a javascript file which contained the appropriate javascript class for the object.

It all seems so simple. What am I missing?

Javascript SVG renderer

So, I’ve been playing a lot with how to draw arbitrary shapes in javascript for a project I’ve been working on. SVG and are both options, but poor options, as neither has great browser support. SVG loks the most promising, as it’s xml, very featureful, and integrates well into the who xhtml paradigm.

The question is, how can you use it now, before the browsers get native SVG support. The adobe plugin is an option, but anything that requires users to download a plugin is relatively worthless in the general internet sphere.

However, why not write an SVG renderer in javascript? It seems like all the pieces are there. Javascript has a good xml parser, and it can output arbitrary raster graphics, via somewhat nasty html/css hacks. You can draw an arbitrary raster image by simply using many 1 pixel div tags.

To go a step further, Walter Zorn’s javascript vector graphics library already has basic vector graphics functions (polylines, ovals, lines, etc), using thse same div hacks. This could be extended or built on to get full svg functionality.

Form validation gone wrong, or while T-Mobile tech support sucks

So, this summer I bought a blackberry. It’s nifty, and since I can get email on it, I now get viagra spam wherever I am. What more could I ask for in a handheld device?

Part of getting the blackberry meant switching to T-Mobile (so I could get a $150 gift certificate with Amazon, which I used to buy most of my wishlist). In doing so, I setup my online T-Mobile account, so I could pay my bill online. Nifty. The password I picked has a comma in it. The registration process let me pick it without a problem. However, when I go to log in, the form returns with an error message stating “Please do not use a space or ‘ or or ; in your password”. Never mind the fact THAT MY PASSWORD HAS A COMMA IN IT! So you’ll let me choose it as my password, just as long as I don’t actually try and login with it.

So, there’s a link beneath this error message saying “Forgot your password?” Hmm, I’ll try clicking on that, and see if maybe they’ll let me reset it. No dice, they just send me the password with the comma. Bastards.

So I call customer support. The lady on the other end of the phone seems to think my account is locked, despite my assurance that she’s going to need to change my password so their stupid web form will let me in. She can’t change passwords.

But tech support can. So she transfers me there. “The current wait time for the next representative exceeds three hours.” You’ve GOT to be kidding me. Maybe this is a sign you need to hire more tech support people. Or at least smarter programmers.

Then I notice a link on the front page of t-mobile.com: “Personalized online support”. Only, when you click on the link, IT WANTS YOU TO LOGIN! Using a password. The one with the comma. The one that it won’t allow.

Bastards.

Google Reader

So, I’ve been playing with Google Reader a bit, and overall, it’s another slick Google AJAX app. However, I agree with Les over at OxDECAFBAD that it doesn’t really meet the needs of people who are used to reading lots and lots of feeds:

Anyway, what I look for in a feed reader is how well it enables speed skimming: I’m going to ignore 70-90% of what I see in feeds, so I don’t want an aggregator which helps me carefully and methodically pick my way across the headlines.No, I want something which lets me scream through feeds as fast as my eyes can move and a finger can slap the space bar. I’ll queue up what I really want to read in browser tabs or some other temporary storage, but I’m not going to want to spend more than a split second on a headline.

I think the biggest thing missing is a sense of overall context. You can only view 8 headlines at a time, and by default, you see all your feedsd aggregated together. Heck, I get far more feed content than email, and I can view way more subject lines in gmail, or any other email or feed reader. The net effect is that it feels like you’re looking at your feeds through a tiny pinhole, when you really want a firehose.

I did notice something interesting about Google Reader, though. It seems to be showing me entries for feeds that I don’t subscribe to. I can’t tell whether this is a bug or a feature, but I’m voting for the latter, as they seem to be stuff I’d be interested in (weblogs.oreilly.com, for one). Maybe they’re doing cool collaborative filtering stuff? It’s not clear.