Hooks or events
Mobase supports the following hooks (events):
- onBeforeValue
- onAfterValue
- onBeforeChildAdded
- onAfterChildAdded
- onBeforeChildChanged
- onAfterChildChanged
- onBeforeChildRemoved
- onAfterChildRemoved
Hooks can be set in different ways:
Via options
const myStore = new MobaseStore({
..., //some options
onBeforeChildAdded({id, data}) {
data.title = uppercase (data.title) // data is a pointer, so altering will affect items added
}
})
myStore.values()[0].title // UPPERCASED
Directly as store methods
const myStore = new MobaseStore(options)
myStore.onBeforeChildAdded = function({id, data}) {
console.log( this.collection.values() ) // this is bound to myStore
data.title = uppercase (data.title) // data can be altered affecting items added
}
or..
class MyStore extends MobaseStore {
onBeforeChildAdded({id, data}) {
data.title = uppercase( data.title) )
}
onBeforeValue({data}) {
//..do something
}
}
const myStore = new MyStore(options)
Directly as model methods
All except onBeforeValue
onAfterValue
onBeforeChildAdded
onAfterChildRemoved
class Todo {
@observable id
@observable title
set $mobaseFields(value) {
this.id = value.id
this.title = uppercase( value.title ) //makes more sense to alter data here
}
onBeforeChildAdded({data}) {
data.title = uppercase( data.title ) //than here
}
onBeforeChildRemoved() {
//this will be removed now!
}
}
const myStore = new MobaseStore({ model: Todo })