JasonWyatt

This blog is full of stuff, and things.

Find my resume.

permalink

Dynamically Instantiate a Dojo Class At Runtime

Need to create an instance of a class in Dojo without knowing what that class is ahead of time? Will you only have the name of the class that you want to instantiate? Check this out:

/**
 * Create a new instance of a Dojo class.
 *
 * @param className
 *    (String) A fully-qualified name for class, such as 
 *    "my.pack.age.Class"
 * @param constructorSettings
 *    (object) An object that will be passed on to the 
 *    class's constructor.
 */
function instantiateDojoClass(className, constructorSettings){
    try {
        dojo.require(className);
        
        /*
         * Eval the class name to get a reference to the 
         * function, then call it as a constructor using the 
         * constructorSettings object as an argument.
         */
        var obj = new (eval(className))(constructorSettings);
    } catch (e){
        /*
         * Handle any errors that might happen.
         */
        if(console){
            console.error("Could not instantiate %s: %s", classsName, e.message);
        } else {
            alert("Could not instantiate "+className+": "+e.message);
        }
        throw e;
    }
}

The real trickery here is in the line:

var obj = new (eval(className))(constructorSettings);

This evaluates the string className to retrieve a reference to the dojo function that was ‘created’ by the call to dojo.require(..), then calls that function as a constructor by using the new keyword. It passes the constructorSettings object on to the constructor, just like you’d do with any normal dojo instantiation.

permalink

HTML 5 Canvas - Mandelbrot Fractal

I’ve been really interested in HTML 5’s new <canvas> feature and have been trying to think of excuses to use it (like on Friday when I made jQReflect). Today I felt it’d be fun to do an old classic: a mandelbrot fractal renderer.

It turns out that JavaScript is still pretty slow, even after all the advances lately in browsers’ JS engine technology. Drawing to the canvas was not my performance bottleneck, however. The sheer number of calculations I had to perform is what slows it down.

Check out the demo.

permalink

Did you know this? (RegExp)

Try doing something like this in a few browsers:

var myRegExp = new RegExp();
alert("typeof myRegExp = "+(typeof myRegExp);

This turns out to be a rare instance where Safari stands out from the crowd and gives a different answer from the others (IE 7, Firefox 3.5, Opera 10). I didn’t get a chance to test it on Chrome, but I’d be curious to know how it acts there and on other WebKit-based browsers - leave it in the comments if you can!

If you’re too lazy to try it on your own, here’s what happens: on Firefox, Opera, and IE 7 typeof someRegExpObject returns "object", however on Safari it returns "function". Weird!

permalink

Getting computed style

nonowarn:

When the computed style of arbitrary element is needed,

var computedStyle = document.defaultView.getComputedStyle(element, null);

should work. But in IE, it doesn’t. But non-tested “cross-browser” version is here:

function getComputedStyle(el) {
   var dview = document.defaultView;
   return dview ? dview.getComputedStyle(el, "") : el.currentStyle;
}

References:

Nicely done! I’ll have to play with this tonight after work..

permalink

Footnotes.js - Automatic Footnote Creator

This is another project I worked on a few months ago. It’s a script that will automatically scan your document for footnote anchors and generate links to the footnotes you specify in an ordered list anywhere on the page. It requires jQuery to operate.

Tutorial

  1. Download the zip and extract it into a folder within your site somewhere. For the sake of this tutorial we’ll say you unzipped it into the directory /httpdocs/footnotes, where /httpdocs is the root of your web site and is where the HTML file we’re going to use footnotes with resides.
  2. In your HTML document’s <head> section add the following lines:

    <link ref="stylesheet" href="./footnotes/footnotes.css" type="text/css" charset="UTF-8" />
    <script type="text/javascript" src="./footnotes/footnotes.js" charset="UTF-8"></script>
  3. There are two main areas to think about when you want to reference a footnote:

    • the text that is referring to the footnote,
    • and the footnote itself.

    With footnotes.js, you refer to a footnote by using an anchor tag and you store your footnotes in a list.

    Depending on the settings you specify within footnotes.js, what you need to do can vary, but we’ll assume you stuck with the default values.

    Assume you have the following paragraph in your HTML (from Jules Verne’s Around the World in Eighty Days):

    <p>
      Mr. Phileas Fogg lived, in 1872, at No.7, Saville Row, Burlington Gardens, the house in which Sheridan died in 1814. He was one of the most noticeable members of the Reform Club, though he seemed always to avoid attracting attention; an enigmatical personage, about whom little was known, except that he was a polished man of the world.  People said that he resembled Byron - at least that his head was Byronic; but he was a bearded, tranquil Byron, who might live on a thousand years without growing old.
    </p>

    And assume we want to add a link to a footnote after the first sentence that says this:

    At least we think it was the house where Sheridan died, but we're not 100% sure.

    As well as another footnote referenced after each mention of Byron that has a link to the Wikipedia article about Byron:

    See the article at <a href="http://en.wikipedia.org/wiki/George_Gordon_Byron,_6th_Baron_Byron">Wikipedia</a> to learn who George Gordon Byron was.

    We would introduce three anchor tags into the paragraph which would look like so:

    <p>
      Mr. Phileas Fogg lived, in 1872, at No.7, Saville Row, Burlington Gardens, the house in which Sheridan died in 1814.<a class="footnote"></a> He was one of the most noticeable members of the Reform Club, though he seemed always to avoid attracting attention; an enigmatical personage, about whom little was known, except that he was a polished man of the world.  People said that he resembled Byron<a class="footnote"></a> - at least that his head was Byronic; but he was a bearded, tranquil Byron<a class="footnote" name="2"></a>, who might live on a thousand years without growing old.
    </p>

    If you notice, you’ll see two different kinds of anchor tags we added there:

    • One with only a class attribute “footnote” - These will get auto-numbered by the script in the order in which they appear. This is useful if you know your references will be in the same order as the footnote list you’re using.
    • One with two attributes: class and name - The class attribute is still set to “footnote”, but the name attribute is identifying the “target” footnote to reference (I would’ve loved to make the attribute be the “target” HTML attribute, but in XHTML Strict there is no such thing).

    Of course we also need to add a list containing the content of our actual footnotes (you can put it anywhere):

    <ol id="footnotes">
      <li class="footnote">
        At least we think it was the house where Sheridan died, but we're not 100% 
        sure.
      </li>
    
      <li class="footnote">
        See the article at 
        <a href="http://en.wikipedia.org/wiki/George_Gordon_Byron,_6th_Baron_Byron">Wikipedia</a>
        to learn who George Gordon Byron was.
      </li>
    <ol>
  4. There we go, you’re done!

permalink

A recording of a lesson I gave to my team on introductory JavaScript. If it’s too small to see the text in the feed, head on over to the Vimeo page here or view it in full screen.

You can also download the .m4v version directly: from Vimeo