How to create a widget inside the "jquery-tmpl"?
Asked Answered
M

2

8

I have to create a widget for the div which is inside the jquery-tmpl script.

<script id="movieTemplate" type="text/x-jquery-tmpl"> 
      <div id='name'>${Name}</div>
</script>

In the above script, I want to access the name div and create a widget.

Note: I am going to append that script in another widget, so here I don't know "what is the parent of this template".

How could I achieve this?

Misgovern answered 20/7, 2012 at 9:36 Comment(0)
A
3

In the code you show, the string <div id='name'>${Name}</div> is inside a script node, so it is not translated into a DOM node, and you cannot access the div node before actually rendering the template.

You can render the node : $.tmpl(myTemplate, myData).appendTo(myDomElement), and then you can access the created node : $(myDomElement).find('div#name') and do whatever you want with it (e.g. : $(myDomElement).find('div#name').datepicker() ...)

Appositive answered 24/7, 2012 at 12:21 Comment(0)
H
0

Templates are just something to make building html strings easier, they are not for logic such as initialization of widgets or whatever.

The code that turns the html into dom is where the widget creation should occur. Your template should indeed not know "what is the parent of this template" since it's just a dumb string.

Hasseman answered 29/7, 2012 at 22:25 Comment(1)
Why the downvote? What I said is the ultimate undeniable truth :)Hasseman

© 2022 - 2024 — McMap. All rights reserved.