-
-
Notifications
You must be signed in to change notification settings - Fork 968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to prototype a tag #1053
Comments
It would be nice to have something like this in riot <my-tag>
<script>
Riot({
created: function(){ console.log("created") },
attached: function(){ console.log("attached") },
detached: function(){ console.log("detached") },
updated: function(){ console.log("updated") }
})
</script>
</my-tag> this way it would execute that code on the top level immediately, and allow us to create and use prototypes, as well as provide a beautiful events API |
I think Riot should move to ES2015 in the future. Then we'll be able to use class MyTag extends riot.Tag {
constructor(opts) { ... }
template() { ... }
style() { ... }
someMethod() { ... }
} |
@cognitom my nippur72/RiotTS does just this, it lets you use To make Riot ES6 compatible, we need |
Ugh. ES6 classes make me cry. Please, kill them before they take over my precious prototypal inheritance. Please *don't use constructors or classes in your API. And please, please don't require If you choose to implement inheritability, use something like Method 3 or 4 shown here |
@pitaj I am going to assume that you were kidding me. |
@steelbrain Well, you'd be assuming incorrectly. Classes, constructors, and any kind of classical inheritance patterns don't belong in JavaScript. It is a prototypal language and should be treated as such. |
@pitaj I am afraid you are misinformed my friend. Javascript is more than just a "prototypal" language. and I am sure that we agree that the people writing ECMA specifications aren't retards, there is a reason they put them in ES6. There is a reason v8 is shipping them, and that reason is that Classes are the way forward. They are so much simpler to use and work with. Besides If a person like me (and I am sure that I am not alone) writes both PHP (+ Hack) and Javascript, both languages feel like home to him. |
PHP ewwwwwwwwwwwwww Classical inheritance sucks, |
@steelbrain how about talking about this again a few month later? This issue seems the matter of Riot 3.0 or more. At least in 2.x we couldn't accept such a big change, I think. And by then, ES6 must be more accepted generally, too. |
@cognitom a few months is a long-long time. but anyways, until then I am using this snippet to "emulate" Polymer like interface window.Riot = function(riot, proto) {
if (proto.properties) {
for (let key in proto.properties) {
if (proto.properties.hasOwnProperty(key)) {
const info = proto.properties[key]
if (info !== true) {
// Use predefined value
riot[key] = info
continue
}
// look in dataset and params
const htmlKey = key.toLowerCase()
if (typeof riot.opts[key] !== 'undefined') {
riot[key] = riot.opts[key]
} else if (riot.root.dataset.hasOwnProperty(key.toLowerCase(htmlKey))) {
riot[key] = JSON.parse(riot.root.dataset[htmlKey])
riot.root.removeAttr('data-' + htmlKey)
} else if (riot.root.hasAttribute(htmlKey)) {
riot[key] = JSON.parse(riot.root.getAttribute(htmlKey))
riot.root.removeAttr(htmlKey)
} else {
throw new Error(`'${key}' parameter not found`)
}
}
}
}
delete riot.properties
extend(riot, proto)
if (proto.attached) {
riot.on('mount', proto.attached)
}
if (proto.detached) {
riot.on('unmount', proto.detached)
}
if (proto.updated) {
riot.on('update', proto.updated)
}
if (proto.created) {
proto.created.call(riot)
}
} <some-tag>
<div>Wow</wow>
<script>
Riot(this, {
created: function(){
console.log("I was created")
console.log(this.SomeValue)
console.log(this.PreDefinedValue)
},
updated: function(){
console.log("I was updated")
},
attached: function(){
console.log("I was attached")
},
detached: function(){
console.log("I was detached")
},
properties: {
SomeValue: true, // it'll extract the value from attributes or the riot mount arguments
PreDefinedValue: "Hey There"
}
})
</script>
</some-tag> now lets mount it with different arguments riot.mount('some-tag', {SomeValue: "asd"})
// > I was created
// > asd
riot.mount('some-tag', {SomeValue: false})
// > I was created
// > false
riot.mount('some-tag')
// > Error<SomeValue parameter not found>
domEl = document.createElement('some-tag')
riot.mount('some-tag')
// > Error<SomeValue parameter not found>
domEl = document.createElement('some-tag')
domEl.setAttribute("SomeValue", '{"key": "value"}')
riot.mount('some-tag')
// > I was created
// > {key: "value"}
// (it automatically tries to json decode it) This gives us a way to define tags that can be constructed/initiated from both the DOM and custom mount. It has also reduced the pain of extracting argument in each tag, now I just have to write that function and I am done. |
Oh, complicated. Why not use |
@cognitom: |
Thanks for your explanation. Can't we keep the |
@steelbrain nice polymer emulation (BTW if you're a Polymer fan, check out my PolymerTS repo). If you want to make it complete, you could override Regarding events like |
With riot 2.3.0 we expose the internal Tag instance. I am closing this issue |
The more I use riot, the more I feel like I am doing this
instead of
is it planned? or can I expect it to be implemented in riot?
The text was updated successfully, but these errors were encountered: