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
Ractive @state RFC #2345
Ractive @state RFC #2345
Conversation
Interesting! Here are my random thoughts thus far: I like the shortcut syntax, and as far as comprehension goes, there is pretty good precedent from handlebars and ruby. My only immediate concern is that computations are referenced as I think this could also open an avenue to resolve some of the confusion around method events as a breaking change. The existing method events could be swapped to the As far as the bits of state that a Ractive instance holds, perhaps it would be best to move them into a I also like the idea of state paths external to the data that live in a sort of shadow data tree. I don't have much use for them personally, but it seems like it would solve some issues for a fair number of others. |
From a zero-internal-knowledge-of-internals point of view:
I'm 👍 , just a few clarifications that may need documentation. |
holy shit, how did I miss this one?! @martypdx I really like this proposal a lot. also, you totally blew my mind with the some thoughts:
and, a question: if I understand you correctly, we can read/modify the state of the root component, but not its data, right? |
can we get others to weigh in on this PR? personally, I'm quite fond of it. I REALLY like the idea of separating data and state like this |
i guess i'll chime in with a description of the 3 phases i go through when using state in ractive. the beginning: man it would be awesome if i could store state outside of the middle: i can't do the above so i guess i'll just segregate the end: crap, i actually need state information on nested objects within so, the misc part at the end of the original comment is actually what interests me the most. and if you stick with the |
7327cdd
to
33e65a9
Compare
With @evs-chris' work on #2250 which introduced the
@ractive
keyword, it did most of the heavy lifting to open the door to a state mechanism that works in along side with ractivedata
.how it works
Because having to use the prefix
{{@ractive.foo}}
would litter the template with extraneous noise, at parse-time, refs that use the format{{@foo}}
are treated as aliases for state on the ractive instance, except for the the reserved keywordskeypath|rootpath|index|key|ractive|global
:The double
@@
is an alias for@ractive.root
, with the intent being global "app" state. For example, if you add auser
property to the root instance, you can access{{@@user}}
anywhere.The api also supports
@foo
, so you can use it in computations or anywhere else you would access keypaths.State behaves differently than data in the following ways:
Given those rules, when passing state between components:
State can be passed into components and become data:
In this case, it is mapped like normal component data and
bar
will update as@foo
updates in the parent and vice-versa.State can be set in components (from either parent state or parent data):
However, when setting state on a component it is copied and not mapped. Object references would of course point to the same object, but would not be updated if changed to another instance in one and not the other. So passing state works as a starting point for the state on the component much more like a traditional js function parameter.
concerns
@state
exists on the instance, so it is possible to munge the ractive api, so perhaps these should be disallowed at parse time.ractive.viewmodel
that probably need to get changed toractive._viewmodel
or added to the disallow list.misc
An additional level of state that could be added would be using a property prefixed with
@
on an existing data model. This would allow it to be associated with that data model keypath without actually writing it to that data. So it would look like:{{ someObject.@foo }}
,{{ this.@selected }}
, etc.