amp-bind can't set state for variables inside amp-state
See original GitHub issueAt the moment it’s only possible to set the values for global variables via AMP.setState but not for variables declared inside amp-state.
Example:
AMP.setState(foo=123)
-> is working
AMP.setState(myAmpState.foo=123)
where
<amp-state id="myAmpState">
<script type="application/json">
{
"foo": "234"
}
.....
is not working. Current workaround is setting default variables.
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Amp-state getting ignored until setState() fired
If I understand correctly, the problem you are facing is amp-bind's are only computed on user action. You have to initialize the initial ......
Read more >Documentation: <amp-bind> - amp.dev
The state variables reverts back to the defined JSON in <amp-state> on page refresh. Event triggering and data. When triggered by certain events,...
Read more >What is Initialize state in amp-bind of Google AMP
In this article, we will talk about the initial state of the elements. The initial of an amp-state variable is always NULL but...
Read more >[amp-bind] I can't get [value] and value attributes default on ...
For that i must bind two buttons and an input. I have tried using quoted and unquoted value in case there was a...
Read more >Initialize state on page load with amp-list - How to AMP
In this episode of How to AMP, we go over how to use the amp-list component to inalized amp - bind state on...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
myAmpState.foo
sounds good. An alternative toAMP.setState()
we could expand grammar with{}
like this:That way we could keep state and actions in separate grammars since functional calls normally not the same as state manipulation.
RE: grammar: by all means. The only question is the size. Last time I tried, I got 9K minified JavaScript and decided to go down the manual path. With
amp-bind
we have an alternative approach, though. We can queue up{}
expressions in actions untilamp-bind
has been downloaded and installed and haveamp-bind
parse them. That could let us be expressive in grammar w/o blowing up core binary size.Hmm, I see. So in the case of resorting to the “default” size,
selectedSize
will have a value inconsistent with what’s displayed on the page.Question: do you need
selectedSize
to always be consistent with the page’s display state? For this page, a form can submit using theamp-selector#size
’s value instead of theselectedSize
variable.I can see how this would be useful though. One way would could support this is to support more powerful syntax in
AMP.setState
as Chiara suggests. Instead of having the logic for setting default size inamp-selector#size
, move it intoAMP.setState
:To support this, I like Dima’s suggestion to allow
amp-bind
to parse these expressions with a special syntax forAMP.setState
. Perhaps:This allows multiple variable assignment and makes it clear that the special syntax only works for the
AMP.setState
action.