ampproject / amphtml

The AMP web component framework.
https://amp.dev
Apache License 2.0
14.89k stars 3.89k forks source link

amp-bind: Allow real JavaScript in expressions #25684

Closed morsssss closed 3 years ago

morsssss commented 5 years ago

<amp-bind> has proved to be a highly flexible and useful mechanism to allow all sorts of interactions in AMP sites! I imagine it’s now being used for all sorts of cases that weren’t anticipated. So here are some reasons I think it would be really impactful to allow developers to reference real JavaScript functions or methods in their expressions… not just JavaScript expressions encased in strings.

Now that we have JavaScript in Workers, this seems possible to me - especially as I'm told that <amp-bind> spins up a Worker for safe JS execution anyway. Could we use the same magic that makes <amp-script> possible?

So, for example:

flights.html

<amp-state id="filters">
            <script type="application/json">
            {
                "origin": "JFK",
                "destination": "WAW",
                "priceHigh": "1000"
            }
           </script>
</amp-state>

<amp-list [src]="fn:getURL(filters)">

script.js

const apiBaseURL = 'https://api.foo.com/wow';

function getUrl(filters) {
    let queryString = '';

    // here lives logic to check filters and construct query string . . .

    return apiBaseURL + '?' + queryString;
}

Or, we could have developers place their JavaScript inside a class or object with a standard name. Etc.

Ok. But... why?

/cc @nainar , @choumx

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.