xrsh-com/com/data2event.js

47 lines
838 B
JavaScript
Raw Normal View History

2024-05-24 16:37:05 +02:00
/*
* ## data_events
*
* allows components to react to data changes
*
* ```html
* <script>
* AFRAME.registerComponent('mycom',{
* init: function(){ this.data.foo = 1 },
* event: {
* foo: (e) => alert("I was updated!")
* }
* })
* </script>
*
* <a-entity mycom data_events/>
* ```
*
*/
AFRAME.registerComponent('data2event',{
init: function(){
setTimeout( () => {
for( let i in this.el.components ){
let com = this.el.components[i]
2024-05-29 18:13:53 +02:00
if( typeof com.data == 'object' ){
com.data = this.reactify( this.el, com.data)
}
2024-05-24 16:37:05 +02:00
}
},50)
},
reactify: function(el,data){
return new Proxy(data, {
get(me,k,v) {
return me[k]
},
set(me,k,v){
me[k] = v
el.emit(k,{el,k,v})
}
})
},
})