6635

Recursive jQuery Templates with custom functions causing stack overflow?

Question:

I'm creating a jQuery Template system that recursively calls the same template as needed. Most of the examples of "recursive" templates are actually just templates calling other templates, even the ones that <a href="https://stackoverflow.com/questions/5263036/jquery-nested-templates-using-options-parameter/5263407#5263407" rel="nofollow">pass around the $item object to retain custom functions</a>. In this case, I want to call the same template with a sub-portion of the original $data (AKA $item.data) while also passing around custom template functions passed in to the options of the original tmpl call.

// original template call $("#someTemplate").tmpl(data, { someFunction1: function(itemToCheck) { return itemToCheck.p3; }, someFunction2: function(itemToCheck) { return itemToCheck.p1 + ", " + itemToCheck.p2; } }).appendTo(".results"); <!--in-template secondary call--> {{tmpl($data.sub, $item) "#someTemplate"}}

<a href="http://jsfiddle.net/patridge/YBFqR/" rel="nofollow">Full code jsFiddle</a>

It seems that passing around $item as the options parameter for the recursive call results in the data parameter being ignored, probably because $item.data contains the original object and it simply overwrites the new data parameter. As a result, at each recursive level, I am still operating at the original caller level in the object and making no further progress in the object structure (hence the stack issue).

Is there some other property of $item I need to use to pass around only the custom functions without having $item.data override the template data being passed in?

<h3>Update</h3>

You definitely cannot just pass $item as the options parameter to {{tmpl}}. After <a href="https://github.com/jquery/jquery-tmpl/blob/master/jquery.tmpl.js#L30" rel="nofollow">looking at the code</a>, it sets data for the new template call and then blows it away with the original $item.data through jQuery.extend a few lines later.

Answer1:

While I cannot seem to find a variable on $item that wraps all the custom functions, I was able to work around this issue by passing in each custom function individually from the original $item object. Not ideal, but it works.

{{tmpl($data.sub, { someFunction1: $item.someFunction1, someFunction2: $item.someFunction2 }) "#someTemplate"}}

Recommend

  • Regex - Match Last Occurance
  • Celery + Redis - .get() hangs indefinitely after running smoothly for ~70 hours
  • ServiceStack Handler Not Found When Periods Present in Path
  • Displaying and sizing a grayscale from a QImage in Qt
  • Context menu disappears when opened with Key.App
  • Get sibling of Vaadin Tree Item?
  • Deserialize Dictionary
  • How do I check if System::Collections:ArrayList is empty / nullptr / null?
  • Creating a C++ function that calls other Lua function
  • How to use function wrapper in mustache.php?
  • Who propagate bugfixes across branches (corporate development)?
  • xcode don't localize specific strings
  • How to assign byte[] as a pointer in C#
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Do I need to reset a Perl hash index?
  • ilmerge with a PFX file
  • Deleting and Updating values from a cusrsor adapter
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Modifying destination and filename of gulp-svg-sprite
  • javascript inside java/jsp code
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Statically linking a C++ library to a C# process using CLI or any other way
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • embed rChart in Markdown
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • JaxB to read class hierarchy
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Running Map reduces the dimensions of the matrices
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Why do underscore prefixed variables exist?
  • How to push additional view controllers onto NavigationController but keep the TabBar?