Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery templates, accessing parent variable from child template

I cannot seem to get hold of the parent varible from the child template, does anyone know how?

Current code:

 <li class="ui-parent-field" >
            <b>${ Name }</b> 
            {{if Options ===null}}
            | <span data-field-name="${ Name }" data-field-type="${ Type }"></span> 
            {{/if}}
            <br/> 
            ${ Description }
            {{if Options !==null}}
            <div style="clear:both;">
            <ul class="ui-child-list">
                {{each Options}}
                    <li class="ui-child-field">
                        ${ Name } : ${ Value } | 
                    <span data-field-type="${ Type }" data-field-name="${ Name }"></span>
//NOTE should be:
<span data-field-type="${ Parent.Type }" data-field-name="${ Parent.Name }"></span>                    </li>
                {{/each}}
            </ul>
            </div>
           {{/if}}        
        </li>

This is a very crude example but I am basically throwing out some info into the view using jquery templates, I have a parent span item which contains Name + Type, then I am throwing out the child elements of this object if they exist, I want to access the Type property from the parent inside the each loop.

like image 210
Haroon Avatar asked May 20 '11 12:05

Haroon


1 Answers

You can access the object that the entire template is bound to using $data. So, you would use something like:

<span data-field-type="${ $data.Type }" data-field-name="${ $data.Name }"></span>  
like image 119
RP Niemeyer Avatar answered Nov 02 '22 09:11

RP Niemeyer