Skip to content

New amp-script samples: multiple fetches, and communicating with an AMP component#4386

Merged
matthiasrohmer merged 6 commits intofuturefrom
feature/more-samples
Sep 7, 2020
Merged

New amp-script samples: multiple fetches, and communicating with an AMP component#4386
matthiasrohmer merged 6 commits intofuturefrom
feature/more-samples

Conversation

@morsssss
Copy link
Collaborator

Check it out!

I was starting to add a sample for protocol adapters, but then as I tested I realized they're still behind a flag.

Copy link
Collaborator

@sebastianbenz sebastianbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great samples! Left a few suggestions how to simplify things.

<button id="multi-fetch">How slow is our API?</button>
</amp-script>
<script id="multi-fetch-script" type="text/plain" target="amp-script">
const randomTime = () => Math.floor(Math.random() * 10) + 5;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make it random? How about making 10 requests where each requests takes 1s longer. This way we can visualize the cut off.

Copy link
Collaborator Author

@morsssss morsssss Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could not be random. I think the randomness part makes it more fun to play with. But also, to me, it implies more clearly how that this works - that your code can make mutations until 5 seconds after the last fetch returns... not, say, after you make your last fetch, which is kind of what is implied to me by a linearly increasing set of delays.

Of course, I could keep on making changes for 4 seconds after the last fetch returns, but that seems to me too much.


function insertText(text) {
const div = document.createElement('div');
div.innerHTML = text;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use textContent instead of innerHTML

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops. old habit.

When your script modifies that state variable, the change will propagate to the component.
-->
<div id="carousel-sample">
<amp-carousel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you simplify this example? We don't need so much markup just to demonstrate the point. A single image with a binding src value would be sufficient.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To you and me, <amp-img> is an AMP component. But I think that to most people it's just a replacement for <img>. And what I wanted to show here was that you can use <amp-script> to supercharge an existing AMP component - to give it new features. That people should really try to do this instead of creating components from scratch in JS.

Of course, your example is super clear. In the new documentation, I do something similar - I just show it's possible to use amp-script to modify text in a <p>.

Maybe I should include your example too - showing an image changing - for a simple example showing how amp-script can interact with the DOM via data binding. Because I don't show that. And then I could include my example to show how it's possible to modify a component's behavior. Thinking about it more, I should even put the "Surprise me" button on top of the carousel, so we really see the component has gained a new feature.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the simpler sample showing an image changing. I think this is an improvement - thanks!

<script id="carousel-script" type="text/plain" target="amp-script">
const button = document.getElementById('carousel-button');

function gotoRandomSlide() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also adds too much complexity. Simply switching between two images is enonugh.

@samouri
Copy link
Member

samouri commented Aug 27, 2020

I was starting to add a sample for protocol adapters, but then as I tested I realized they're still behind a flag.

The PR to activate the feature in production was merged > 20 days ago (1), but the cut has been delayed due to a P0 Ads issue that I believe was resolved today. It should make it to production before weeks end

@matthiasrohmer matthiasrohmer merged commit 733bfe0 into future Sep 7, 2020
@matthiasrohmer matthiasrohmer deleted the feature/more-samples branch September 7, 2020 14:16
robinvanopstal added a commit that referenced this pull request Sep 7, 2020
* future:
  New amp-script samples: multiple fetches, and communicating with an AMP component (#4386)
  Update dependency husky to v4.3.0 (#4443)
  Update dependency node-fetch to v2.6.1 (#4441)
  Hide results if page is not amp (#4442)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants