Bug Tracker

Modify

Ticket #8041 (closed bug: invalid)

Opened 2 years ago

Last modified 2 years ago

jquery.tmpl plugin's tmplItem not returning expected item

Reported by: jdart@… Owned by: BorisMoore
Priority: low Milestone: 1.next
Component: templates Version: 1.4.4
Keywords: Cc:
Blocking: Blocked by:

Description

If you render a template using an each and then access the dom item (for example in a live/delegate event handler) tmplItem returns the parent data structure as opposed to the desired item in the array:

//returns
{
    items: [
        {num: 1}
    ]
} 
//instead of 
{
    num: 1
} 

See  http://jsfiddle.net/cKA3s/2/ Ideally, items inside of {{each}} should be considered nested templates

Change History

comment:1 Changed 2 years ago by rwaldron

  • Owner set to BorisMoore
  • Status changed from new to assigned
  • Component changed from unfiled to templates

comment:2 Changed 2 years ago by BorisMoore

  • Status changed from assigned to closed
  • Resolution set to invalid

This is by design.

Under container2 there is only one rendered template instance. {{each}} does not create new nested template instances.

The feature you suggest is already available though, if you want it. Just replace {{each items}} by {{tmpl(items) "#tmpl3"}} with tmpl3 using the content you currently have within the {{each}} tag - <li>${ num }</li>.

{{tmpl}} is for template composition. {{each}} is for iteration within a template without creating sub-templates.

comment:3 Changed 2 years ago by jdart@…

Thanks for the note on tmpl. Unfortunately, tmpl does not have access to $index in the same way {{each}} does, or at least not in my testing. Is there a way to access index or would this have to be a plugin to the templating system to create a hybrid of the two tags?

comment:4 Changed 2 years ago by BorisMoore

There are a few ways you can work with index in nested templates. See  https://github.com/jquery/jquery-tmpl/issues#issue/28 and http://bugs.jquery.com/ticket/7137 on reasons why it is not exposed, and some suggested approaches. Also status on whether that feature may later be added to jquery-templates.

comment:5 Changed 2 years ago by jitter

  • Priority changed from undecided to low

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.