Hydrate and compute values reactively from other (server side rendered/generated) HTML signals via local script tags.
npm install be-computedhtml
...
onload="isHappy && !isWealthy"
be-computed='from onload expression, passing in |isHappy, |isWealthy.'
>
`
What this does:
1. Since the onload attribute expression doesn't start with export const ..., and doesn't start with an open parenthesis, be-computed wraps the expression like so:
`JavaScript
export const expr = async ({isHappy, isWealthy}) => {
return isHappy && !isWealthy;
}
`
2. Since the return statement returns a primitive, it applies the value to the adorned element, based on context. In this case, it sets:
`html
`
if the conditions are met, and attaches the be-value-added enhancement.
The value of the computation can be obtained via oLink.beEnhanced.beValueAdded.value.
Example 1b -- Verbose notation with external script tag
`html
...
`
Advantages of using script element -- less issues with characters that cause problems inside an attribute, may get better IDE support. Disadvantages -- a little further away, a little more verbose, if you need to move the element, need to remember to move the associated script element along with it.
Example 1c -- compact notation with inline expression
`html
...
onload="isHappy && !isWealthy"
be-computed='from |isHappy, |isWealthy.'
>
`
Example 1d -- compact notation with external script tag
`html
...
`
Example 1e -- bind to named elements and id'd elements
`html
`
Example 1f -- Add more context to the scripting
`html
`
Since the expression starts with open parenthesis, wrapping is more lightweight. Just adds export const default.
Example 1g
Specify export symbol
`html
`
This allows for multiple expressions that can be used by different enhancements.
Example 1h -- Values coming from host.
`html
#shadow
`
The slash is optional, so this will also work:
Example 1i -- Values coming from host, take II.
`html
#shadow
`
Example 1j
Value coming from marker
`html
`
Example 2a Assigning objects, verbose notation, external script
`html
`
Detecting such expressions: Starts and ends with ({...}), no arrow. If need to use arrow functions inside, need to provide the context.
Example 2b Assigning objects, compact notation, external script
`html
`
Example 2c Assigning objects, verbose notation, inline attribute
`html
`
Example 2d Assigning objects, compact notation, inline attribute
`html
`
Example 2e Assigning object to sub property [TODO]
Example 2f Assigning object to be-scoped enhancement
This would allow transforms to be based on.
We can assign the result of a computation to an enhancement, or the "stateProp" property of an enhancement (as defined in the static config property of the enhancement). So one prominent use case is assigning to local "scope" of an element:
`html
Viewing Your Element Locally
Any web server that can serve static files will do, but...
1. Install git.
2. Fork/clone this repo.
3. Install node.js.
4. Open command window to folder where you cloned this repo.
5. > npm install
6. > npm run serve
7. Open http://localhost:3030/demo/ in a modern browser.
Running Tests
`
> npm run test
`
Using from ESM Module:
`JavaScript
import 'be-computed/be-computed.js';
`
Using from CDN:
`html
``