[Developers] mjt html generation

Nick Thompson nix at metaweb.com
Tue Jul 3 02:50:26 UTC 2007


nope - all the templates are expanded into a single string, which is
then pasted in using innerHTML.  mjt.script="" is evaluated as the
string is being created, well before it gets turned into DOM elements.
this is usually what you want, but i've run into the situation you're
describing too.

my way of dealing with it has to set up a short timeout - in most cases
'bar' should be ready immediately after the current mjt.run() completes,
and the javascript interpreter won't run timeouts until then.  this
doesn't feel clean or robust but it's worked for me so far.

in the future i'm planning to add something like mjt.script="ondomready"
to declare that the script should execute after the dom is ready, but
before mjt.run() returns.  this should be simpler to use, more reliable,
and more efficient.  but i'm only going to be on email sporadically for
the next few weeks, so it will probably show up in mjt 0.6 sometime in
early to mid-august.

     nick

Dae Park wrote:
> Is the mjt template generation synchronous? For example if I call a 
> mjt.def, will I be able to access the element created in the mjt.def in 
> a mjt.script:
> 
> <div mjt.def="foo()">
>   <p id="bar">Hello world</p>
> </div>
> <div>
>   ${foo()}
>   <pre mjt.script="">
>     alert(document.getElementById("bar"));
>   </pre>
> </div>


More information about the Developers mailing list