You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
varvm=newVue({// omittedevents: {'object:creating' : function(args){// omittedreturnfalse;},'object:created' : function(args){// omitted}}});varchild=newVue({parent: vm,// omittedmethods: {performWhatever: function(args){//},createObject: function(args){// Pre-createif(this.$dispatch('object:creating',args)===false){// Stop the creation}// The business logicthis.performWhatever(args);// Post-createthis.$dispatch('object:created',args);}}})
As you can see, the pre-create operation is optional.
Thank you.
The text was updated successfully, but these errors were encountered:
Using the event system ($dispatch, $emit and so on) means your code will be asynchronous, which means what happens next is decided by the message receiver, so you don't write something like SomeAsyncCode === false
In the javascript world, we use callbacks to handle asynchronous logic (Promises are better but it's not applicable here), and $dispatch expects you to pass multiple arguments including a callback function.
Therefore, this is what you actually need:
this.$dispatch('object:creating',args,function(){// The business logicthis.performWhatever(args);})
and on the receiver's side,
events: {'object:creating' : function(args,callback){// Do something with args and fire callback (or not)callback()}}
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.As you can see, the pre-create operation is optional.
Thank you.
The text was updated successfully, but these errors were encountered: