Tags: MochiKit
Javascript Interpreter
2008/07/08 @ 19:45I wanted a convenient way to test out some javascript by running it in a browser and being able to play with it via an interpreter like python has. As it turns out the almighty Bob created a nice interpreter for playing around with Mochikit but I wanted something a bit more generic that would allow me to import any kind of javascript and play with it. It turns out this is really easy so I added one simple function to the interpreter.js file called importjs.
You just call importjs like so
| importjs(url) |
This will import a javascript file from the url given. It's a very simple function that just adds a script tag to the document wrapped in div tag. You can load up the interpreter and type "importjs" to see the code.
| function (jssource) { | |
| importdiv = DIV(); | |
| importdiv.innerHTML = "Importing " + jssource + " <script type='text/javascript' src='" + jssource + "'></script>"; | |
| writeln(importdiv); | |
| } |
Check out the slightly modified interpreter here
MochiKit does makes java suck less
2007/12/21 @ 22:58So the last few days I've been playing around with MochiKit and working with Javascript. Until now I have done some Javascript here and there but not too much. MochiKit seems to make it a lot easier by providing you with lots of useful functions for things you do often. In fact it's so popular that I have a hard time explaining to myself why I hadn't tried to use it up until now. I'm certainly not on the bleeding edge here.
Anyway, like I said, MochiKit makes JavaScript less painful. I have a little mockup for a page that defers going to the server until the user has stopped entering data for 3 seconds. That cuts down on a lot of back and forth between the server and client. It's easy in MochiKit. Just use the callLater function.
| update: function() { | |
| if (this.deferred) { | |
| this.deferred.cancel(); | |
| log('Previous deferred cancelled.'); | |
| } | |
| | |
| if (this.request) { | |
| this.request.cancel(); | |
| log('Previous request cancelled.'); | |
| } | |
| | |
| log('updated'); | |
| this.deferred = callLater(3.0, bind(this.deferredupdate, this)); | |
| } |
In this example the callLater is used to call another function, deferredupdate, after 3 seconds. However, if the user enters data a second time before the three seconds are up then the deferred object will be cancelled and a new deferred object will be created. This has the effect of not calling the update function until a user is really done entering data.
The request object is created in the deferredupdate function.
| deferredupdate: function() { | |
| log('Loading document'); | |
| this.deferred = null; | |
| | |
| this.request = loadJSONDoc('domains.json'); | |
| this.request.addCallback(bind(this.pageupdate, this)); | |
| } |
The deferredupdate function calls loadJSONDoc which creates a deferred object as well however this deferred object doesn't wait for any time it simply does it's work in a separate thread and executes a callback when it's done. In this case I set the callback to be the pageupdate function. The pageupdate function does the work of putting the resulting data into a table and adding that table to the html DOM.
If you are wondering what the bind function does, it always ensures that the 'this' reference works. I'll leave the full explanation for another post.
Most people who program in JavaScript already know about MochiKit but if you are interested in it check out the screencast at http://www.mochikit.com/
Bob Ippolito
2007/12/17 @ 14:49I found out today that the principle author of a popular Javascript/AJAX library Mochikit is my former highschool classmate Bob Ippolito. I remember him being pretty smart. I supposed he would make a name for himself in some CS circles and it looks like he has. He is also apparently a co-founder of Mochi Media, a company that makes products for content creators. Mochibot can be used to track usage of Adobe Flash content. It looks like they are moving into some advertising space by creating an advertising system for casual online games.
Interesting that Bob has moved into this area. Mochikit looks like it's had some extensive work done on it in the past few years so it looks like it's matured well but I imagined a bit more technical, low level path like kernel or low level API development for Bob rather than the relatively high level application/library development in javascript/web applications. I guess everyone follows the buck in the end to some extent and the money is in web dev.









