Created
October 16, 2021 09:18
-
-
Save burakozturk16/fb4bfea5d4fd78a4714440807db36b3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a test prototype coding for planning a react event-bus system | |
*/ | |
class eventBus { | |
static on(event, callback) { | |
document.addEventListener(event, (e) => callback(e.detail)); | |
} | |
static dispatch(event, data) { | |
document.dispatchEvent(new CustomEvent(event, { detail: data })); | |
} | |
static remove(event, callback) { | |
document.removeEventListener(event, callback); | |
} | |
} | |
class baseComponent extends eventBus { | |
constructor(){ | |
super(); | |
} | |
} | |
class myComponent1 extends baseComponent{ | |
constructor() { | |
super(); | |
eventBus.on('duydum', this.soyledim); | |
eventBus.on('gordum', this.gosterdim); | |
} | |
soyledim(){ | |
alert('onu nasıl sevdgimi') | |
} | |
gosterdim(){ | |
alert("1 resim gösteriyor.") | |
} | |
} | |
class myComponent2 extends baseComponent{ | |
constructor() { | |
super(); | |
eventBus.on('gordum', this.gosterdim); | |
} | |
gosterdim(){ | |
alert("2 resim gösteriyor.") | |
} | |
} | |
class parentComponent extends baseComponent { | |
constructor(){ | |
super(); | |
} | |
islemYap(data){ | |
eventBus.dispatch('gordum', data) | |
} | |
} | |
var child1 = new myComponent1(); | |
var child2 = new myComponent2(); | |
var app = new parentComponent() | |
app.islemYap('gordum'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment