I have a vue2 component, where some methods are pretty long with a lot of callbacks and I would like to structurize it better. I try to follow the guidelines as per callbackhell.com but things don't look that easy within vue method.
Below is the sample of where I am at the moment, it works, but I am confused about the following:
Function hoisting doesn't seem to work here, I need to define my functions first before I run them otherwise, it triggers an error. Why is that, am I missing something here?
When I define inner functions like this, I lose the "this" bound to Vue component in them. I fix it by the
.bind(this)
after the function definition, it works but looks rather obscure. Is there any better way to define a utility function within methods while still keeping the context of "this"?Is this generally adding functions into method a good approach in Vue? I know I could make them sibling methods directly in
methods: {}
object and that resolves most of my issues, but as they are only relevant to thesaveFavourites()
method and not to anything else in the component, wrapping them within looks cleaner to me?
Thanks so much
methods: {
saveFavourites() {
var promptName = function() {
return this.$swal({
text: 'Enter name'),
content: 'input',
button: {
text: 'Save'),
closeModal: true,
},
})
}.bind(this);
var storePlan = function(name) {
if(!name || (name = name.trim()) === '' ) return;
axios.post('/api/user-store-daily-favourites', { meals: this.mealIds, name: name })
.then(response => {
if(response.data.status === 'success') {
this.$emit('dailyFavouritesUpdated');
}
});
}.bind(this);
// running it - if I move this above functions definitions, I get error
promptName()
.then( (name) => storePlan(name) );
},