You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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}}
The text was updated successfully, but these errors were encountered:
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.
@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.
Description
When writing a component template, I think we should have a
self
keyword, distinct fromstache
'sthis
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 withincan.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 inevents
, 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 theself
variable in JavaScript (which is meant to refer directly towindow.self
), and prevents conflict withthis
, whichcan.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
The text was updated successfully, but these errors were encountered: