Skip to content

Create "protocol adapters" to allow client-side data massaging #25719

@morsssss

Description

@morsssss

//cc @alankent , to whom credit for this idea is due.

With AMP, it's best to perform as much logic on the server side as possible. This can create a better user experience; their content gets loaded quickly and can be displayed immediately, with minimal preprocessing in the client!

Frequently, though, front-end developers find themselves using APIs they don't control. Of course, this happens when someone uses an API maintained by another organization, but it also happens all too often within a single organization of decent size. Front-end people can't always make the back-end folks return data that's ready for display by a mustache template.

Thus, it would be wonderful if an <amp-list> could make use of user-created JavaScript that would live within a Worker. Borrowing the mechanism that's already been lovingly and carefully created for <amp-script>, a front-end developer could perform light massaging of data that comes from the server before it gets to a mustache template.

Let's say your API returns a price that's a floating-point value. You always want to display two decimal places, especially if it returns a value like 3.9. Wouldn't it be nice to be able to call a helper function like this one?

function formatPrice(num) {
  return num.toFixed(2);
}

Or, let's say you want to display a set of user comments, but only if there's more than one. You can't do that with a mustache template, but some simple logic could make this happen.

See also #23530, where this idea is referenced... and #25684 , which proposes to use a similar helper function mechanism (or a standard class containing helper methods) for <amp-bind>.

//cc also @choumx , @nainar , @pbakaus , @sebastianbenz , @kristoferbaxter

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions