janl/mustache.js

How about expose all the scopes to the js function?

wufeng87 opened this issue · 1 comments

image

  change 
  value = value.call(this.view);
  to
  value = value.call(this.view, this.parent);
pvx- commented

👍 I just wanted to add my own issue for this :).

I think only view is really needed, to access values outside current scope (parent/root) from view function.

Access outside scope from template itself - as discussed in #399 - is problematic, because it breaks compatibility, but functions must be JS-specific anyway, so could it be done there?

I currently use very simple patch of mustache.js (4.2.0) on line 471:

value = value.call(this.view, context.view)

This allows me to do this:

node = {
  id: 1,
  children : [
      { id : 2 },
      { id : 3 }
  ],
  root_id: function(view) { return view.id }
}

with template

{{id}} {# will output node.id #}
{{#children}}
    {{id}}  {# will output node.children[i].id #}
    {{root_id}} {# will output node.id #}
{{/children}}

(Of course real world situations usually need to access some flag or value to perform calculations with.)

I'm not sure if this change is safe/reliable and I don't really care in which way it would be implemented, just having the possibility to access "root" view from function would really help.