[Developers] [mjt] Accessing the current DOM element in mjt.script

Nick Thompson nix at metaweb.com
Tue Sep 8 21:35:26 UTC 2009


Raphael Jolivet wrote:
 > In a "mjt.script" tag, I would like to reference the current element
 > dynamically, to attach some user-defined properties to it.

this is a little trickier than you would expect, because
mjt templates generate innerHTML rather than generating dom
elements one-by-one (this is necessary for speed).
by default, mjt.script code is run as the html is generated,
so the dom element doesn't actually exist yet.

however, you can use a mjt.script="ondomready" attribute to
delay your script until after the template text has been
converted to dom elements.  you will need to set up unique
id= attributes so that you can find them when running your
jquery code.  so you want something like this (untested):

  <ul mjt.for="(pageIdx=1; pageIdx <= nbPages; pageIdx++)">
     <li id="pageitem-$pageIdx">
            Page:$pageId
            <pre mjt.script="ondomready">
                $('#pageitem-' + pageIdx).get(0).pageIdx=pageIdx;
            </pre>
      </li>
  </ul>

hope that helps,

    nick

Raphael Jolivet wrote:
 > Hi,
 >
 > In a "mjt.script" tag, I would like to reference the current element
 > dynamically, to attach some user-defined properties to it.
 >
> I have tried something like
> 
> <ul mjt.for="pageIdx=1; pageIdx <= nbPages; pageIdx++)">
>    <li>
>           Page:$pageId
>           <pre mjt.script="" >
>               this.pageIdx=pageIdx;
>           </pre>
>     </li>
> </ul>
> 
> But it does not work.
> "this" points to an unrelevant template object.
> 
> It would be very useful to have it.
> In combination with jQuery, I could access the parents of the current 
> element and attach some events, properties.
> 
> Thanks in advance for your help.
> 
> Kind regards,
> 
> Raphael
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Developers mailing list
> Developers at freebase.com
> http://lists.freebase.com/mailman/listinfo/developers


More information about the Developers mailing list