question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

amp-bind can't set state for variables inside amp-state

See original GitHub issue

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

@choumx @ericlindley-g

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dvoytenkocommented, Feb 7, 2017

myAmpState.foo sounds good. An alternative to AMP.setState() we could expand grammar with {} like this:

<button on="tap:{myAmpState.foo = bar}"

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 until amp-bind has been downloaded and installed and have amp-bind parse them. That could let us be expressive in grammar w/o blowing up core binary size.

0reactions
dreamofabearcommented, Feb 14, 2017

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 the amp-selector#size’s value instead of the selectedSize 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 in amp-selector#size, move it into AMP.setState:

<amp-selector name="color" [selected]="selectedColor" 
on="select:AMP.setState(
    selectedColor=event.targetOption,
    selectedSize=(product[event.targetOption].sizes.includes(selectedSize)
        ? selectedSize 
        : product[event.targetOption].defaultSize)
")>

To support this, I like Dima’s suggestion to allow amp-bind to parse these expressions with a special syntax for AMP.setState. Perhaps:

on="tap:AMP.setState({
    selectedColor: event.targetOption,
    selectedSize: (product[event.targetOption].sizes.includes(selectedSize)
        ? selectedSize 
        : product[event.targetOption].defaultSize)
})"

This allows multiple variable assignment and makes it clear that the special syntax only works for the AMP.setState action.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found