uci-utils-rx-class/examples/traverse.js

60 lines
2.1 KiB
JavaScript

import RxClass from '../src/rx-class.js'
class Example extends RxClass {
constructor(opts={}){
super(opts)
this.test = {
tomrrow: {bar:'fight', foo:'fighters'},
today: {bing:'bing', bong:{who:'first',what:'second'}}
}
}
}
const example = new Example({
_rx_ : {
handler: value => {console.log('-----------default subscription handler-------', value)},
skip: 1
}
})
example.foundation = null
console.log('-----EXAMPLE: TRAVERSE OF EXISTING OBJECT OR WHEN SETTING A PLAIN OBJECT-----------')
console.log('instance before doing rx on properties\n')
console.log(example)
console.log('--------------------------')
// example.on('changed', (prop) => console.log('emitted----a property changed-----',prop))
// example.on('test', (val) => console.log('emitted value of test',val))
console.log('traversing exitings object \'test\' and making all leaves reactive')
console.log(example.test)
example.rxAdd('test',{traverse:true})
console.log('adding new property which is made reactive by default \'test.today.bong.why\'')
example.set('test.today.bong.why','do not know')
console.log('------\'test\', raw object ------ \n', example.$get('test'))
console.log('------ \'test\', values object ------\n>', example.get('test'))
console.log(example)
console.log('now removing reactivty from \'test\'')
example.rxRemove('test',{confirm:true})
console.log(example.$get('test'))
console.log('-------------------------')
console.log('now add a completely new object to root of \'foundation\' and make it reactive')
const obj = {giskard:'daneel', hari: {name:'seldon', planet:'Helcion'}}
example.set('foundation',obj)
console.log('------\'foundation\', raw object ------ \n', example.$get('foundation'))
console.log('------ \'foundation\', values object ------\n>', example.get('foundation'))
console.log('--------instance after doing rx----\n')
console.log(example)
console.log('--------------------------')
console.log('now change a reactive property directly like')
console.log('example.foundation.giskard = \'daneel a robot\'')
example.foundation.giskard = 'daneel a robot'
console.log('the new value extracted by the getter is',example.foundation.giskard)