Hi, I'm a little confused about the vm.$dispatch
since I expected it to return values from the listener's callback for further logic which could not be placed in the callback _(imo)_ but it turned out to be the instance it self. I'm learning Vue and probably missed something.
var vm = new Vue({
// omitted
events: {
'object:creating' : function (args) {
// omitted
return false;
},
'object:created' : function (args) {
// omitted
}
}
});
var child = new Vue({
parent: vm,
// omitted
methods: {
performWhatever: function (args) {
//
},
createObject: function (args) {
// Pre-create
if ( this.$dispatch('object:creating', args) === false ) {
// Stop the creation
}
// The business logic
this.performWhatever(args);
// Post-create
this.$dispatch('object:created', args);
}
}
})
As you can see, the pre-create operation is optional.
Thank you.
SomeAsyncCode === false
$dispatch
expects you to pass multiple arguments including a callback function.this.$dispatch('object:creating', args, function() {
// The business logic
this.performWhatever(args);
})
and on the receiver's side,
events: {
'object:creating' : function (args, callback) {
// Do something with args and fire callback (or not)
callback()
}
}
Thanks @fnlctrl for the explanation :)
@fnlctrl Thank you for explaination. It's asynchronous, I forgot, how embarrassing =D
Most helpful comment
SomeAsyncCode === false
$dispatch
expects you to pass multiple arguments including a callback function.and on the receiver's side,