BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.

Home Page:http://www.jsviews.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested props loop is not picking up properties of a function

Kwooda opened this issue · comments

commented

In JavaScript, functions are first-class objects, meaning they can have properties. I have an environment where modules are defined by adding a function to a namespace - the function is the module's primary entry point, but the function, itself, may have many other properties, many of which may be functions, themselves.

I have a nested {{props}} loop in a template to iterate over all the modules, and the inner {{props}} loop to list all the properties of a module (function). But the inner loop is not listing any properties. If a property on the outer loop resolves to an object, the inner loop lists its properties - the inner loop just isn't listing any properties if the outer property is a function.

	<div>NS Modules:</div>
	{{props $env.module}}
		<div class="item">
			<span class="label">{{>key}}{{if ~root.NS[key].$module}} (public){{/if}}</span>

			{{props ~root.NS[key]}}
				{{if key != '$module'}}
				<div class="api">
					<span>{{>key}} = {{>~root.$var(prop)}}</span>
				</div>
				{{/if}}
			{{/props}}

		</div>
	{{/props}}

Even when ~root.NS[key] resolves to an object (not a function), it's not listing any of the function properties of that object. I even tried setting noFunctions=false.

Thanks for providing this, but I wasn't able to test the above sample... If you think that this is a bug in JsRender, or a missing feature, and not simply a bug in your sample, can you provide a minimalist sample to repro the problem? The above sample includes some irrelevant content (e.g. {{if}}) tags etc.) and is incomplete (no data provided) so can't be directly tested.

The best would be to create a jsfiddle example with an outer {{props}} loop and an inner one, with simple data, and nothing else - and indicate what you consider would be expected behavior, as compared to the actual behavior.

Given that I will investigate.... Thanks!

commented

Well I tried this:

<script id="myTmpl" type="text/x-jsrender">
  {{props foo}}
    {{:prop}}
    {{props ~root.bar}}
      {{:prop}}
    {{/props}}
    {{props ~root.ob}}
      {{:key}}
      {{:prop.is}}
    {{/props}}
  {{/props}}

</script>

<div id="page"></div>

<script>
  var data = { 
    foo: function(){ return "XX"},
    bar: function(){ return "XX"},
    ob: {sub:function(){ return "XX"}}
   };
  data.ob.sub.is="YES";
  data.foo.a="A";
  data.foo.b="B";
  data.bar.x="X";
  data.bar.y="Y";
  var myTmpl = $.templates("#myTmpl"),
    html = myTmpl(data);
  $("#page").html(html);
</script>

and the output was "A X Y sub YES B X Y sub YES " (modulo white space)

So function objects and properties seem to work correctly with nested {{props}} loops.... I'm not seeing any issue, so far...

My testing indicates that {{props}} works correctly, including when applied to a function (as object) and when some of the properties are of type function. So I will close this issue.

If you have an example which indicates otherwise, can you reply here, with the example code and data, and and we can re-open this issue... Thanks...