Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How about expose all the scopes to the js function? #819

Open
wufeng87 opened this issue May 10, 2023 · 1 comment
Open

How about expose all the scopes to the js function? #819

wufeng87 opened this issue May 10, 2023 · 1 comment

Comments

@wufeng87
Copy link

wufeng87 commented May 10, 2023

image

  change 
  value = value.call(this.view);
  to
  value = value.call(this.view, this.parent);
@pvx-
Copy link

pvx- commented Sep 24, 2023

👍 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@wufeng87 @pvx- and others