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

Proposal: self keyword to go straight to component scope in template #1360

Closed
zkat opened this issue Dec 8, 2014 · 4 comments
Closed

Proposal: self keyword to go straight to component scope in template #1360

zkat opened this issue Dec 8, 2014 · 4 comments
Milestone

Comments

@zkat
Copy link
Contributor

zkat commented Dec 8, 2014

Description

When writing a component template, I think we should have a self keyword, distinct from stache's this keyword, which always goes to the component the template is for, regardless of what context we're in. Again, note, this will only be available within can.Component templates.

Justification and Notes

This feature will be much nicer with the lexical semantics from #1069 in place, as well, since we can simply look at a template and know what self will refer to without having to imagine weird magic underneath.

Since we tend to use the component scope as our main data storage for components, it ends up being useful to be able to go straight there for reading and managing overall view state.

As we start using can-click more instead of click handlers in events, we'll also run into more situations where methods might possibly conflict, so always being able to navigate up the scope and unambiguously call a scope method can be really nice.

I have been using a patch like this at a client project pretty successfully -- combined with can-click arguments, it means I can manage simple bits of application state without ever writing any javascript: can-click="{self.attr 'view' 'someView'}".

Regarding the self keyword itself, I think it's appropriate because it's a word that mirrors the self variable in JavaScript (which is meant to refer directly to window.self), and prevents conflict with this, which can.stache already uses -- otherwise I would've preferred using that. self, this, allows us to have a similar "this is me" keyword without conflicting with most existing code.

Example

{{#with somethingElse}}
  {{#each blargh}} {{! Assume blargh objects have a .setActive method }}
    {{self.name}} {{self.setActive}} {{setActive}}
  {{/each}}
{{/with}}
@zkat
Copy link
Contributor Author

zkat commented Dec 9, 2014

Looking around on http://handlebarsjs.com/reference.html, I saw that handlebars uses @root for something very similar to this that I think would fit this role just fine.

@wclr
Copy link
Contributor

wclr commented Dec 11, 2014

@root is more appropriate of course

@justinbmeyer
Copy link
Contributor

@root is off the table b/c we are using it for the "highest level" context (the application's view model). We make this available even with leakScope: false so components that want to interact with server-side rendering have access to the application model to register if they are doing something async that should be waited for.

I think %self will work.

@justinbmeyer
Copy link
Contributor

Closing for: canjs/can-stache#12

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

No branches or pull requests

3 participants