Comments (5)
What you're seeing is a placeholder that is passing the data and its type information (1__number
), this is correct. What it sounds like you are trying to do is insert the value into the html instead of pass it as a reference. To insert the value just cast it down to a string, for example ...<p>The numeric value is ${String(myVariable)}</p>
.
import Tonic from 'https://cdn.jsdelivr.net/npm/@socketsupply/[email protected]/+esm'
const numberValue = 1;
const booleanValue = true;
const stringValue = 'lorem ipsum';
class MyComponent extends Tonic {
render() {
return this.html`<ul>
<li>Number ${String(numberValue)}</li>
<li>Boolean ${String(booleanValue)}</li>
<li>String ${stringValue}</li>
</ul>`;
}
}
Tonic.add(MyComponent);
from tonic.
I’ve tried to look at the source code, but I am not sure how is it supposed to work:
First, in connectedCallback
the HTML attributes of the <web-component foo="bar"></web-component>
are iterated, and these are collected into this.props
. However, the way they are tested expects placeholder values (like 1__float
, true__boolean
). Why would value of an attribute be a placeholder value at this point? We have just connected the web component, where would a replaced value come from?
for (const { name: _name, value } of this.attributes) {
const name = cc(_name)
const p = this.props[name] = value
if (/__\w+__\w+__/.test(p)) {
const { 1: root } = p.split('__')
this.props[name] = Tonic._data[root][p]
} else if (/\d+__float/.test(p)) {
this.props[name] = parseFloat(p, 10)
} else if (p === 'null__null') {
this.props[name] = null
} else if (/\w+__boolean/.test(p)) {
this.props[name] = p.includes('true')
} else if (/placehold:\w+:\w+__/.test(p)) {
const { 1: root } = p.split(':')
this.props[name] = Tonic._children[root][p][0]
}
}
After this, the values passed to html
are replaced with placeholders:
html (strings, ...values) {
const refs = o => {
if (o && o.__children__) return this._placehold(o)
if (o && o.isTonicTemplate) return o.rawText
switch (Object.prototype.toString.call(o)) {
case '[object HTMLCollection]':
case '[object NodeList]': return this._placehold([...o])
case '[object Array]': {
if (o.every(x => x.isTonicTemplate && !x.unsafe)) {
return new TonicTemplate(o.join('\n'), null, false)
}
return this._prop(o)
}
case '[object Object]':
case '[object Function]':
case '[object Set]':
case '[object Map]':
case '[object WeakMap]': return this._prop(o)
case '[object NamedNodeMap]':
return this._prop(Tonic._normalizeAttrs(o))
case '[object Number]': return `${o}__float`
case '[object String]': return Tonic.escape(o)
case '[object Boolean]': return `${o}__boolean`
case '[object Null]': return `${o}__null`
case '[object HTMLElement]':
return this._placehold([o])
}
if (
typeof o === 'object' && o && o.nodeType === 1 &&
typeof o.cloneNode === 'function'
) {
return this._placehold([o])
}
return o
}
const out = []
for (let i = 0; i < strings.length - 1; i++) {
out.push(strings[i], refs(values[i]))
}
But there seems to be nothing to replace these placeholders now. They are directly inserted into DOM at this point.
Am I doing something wrong?
from tonic.
Thank you! I’d be happy to contribute docs if you give me some guidance!
from tonic.
Feel free to close 👍
from tonic.
Thank you! I’d be happy to contribute docs if you give me some guidance!
I think in the getting started guide there should be a mention that says something like "anything that isn't a string type will be considered a value that you are trying to pass as a reference."
from tonic.
Related Issues (20)
- Revise public/private methods
- Check that second parameter of Tonic.add is a kebab-case string (if provided)
- Async functions are stringified HOT 1
- Multiple class names are not added correctly HOT 3
- Tonic Theme HOT 1
- Cannot pass a `Map` instance as a property HOT 2
- Congratulations! HOT 2
- Composition Issues HOT 6
- Slots HOT 3
- [Question] FAQ? How to use? HOT 2
- When rerending, input focus is lost HOT 2
- Mangling error fixed with Terser, not UglifyJS
- this.children gets overwritten by Tonic changes HOT 3
- Docs request — Can render a component assigned to a variable? HOT 1
- npm install fails
- SSR docs
- Home page has no links to the code or examples HOT 2
- docs in README.md
- Release Tonic 15.0.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tonic.