I am making a simple SPA application in vue.js and I use some jQuery methods. Problem is, I can't use vue's methods inside of jQuery statement. For example: I implemented easyAutocomplete pack for auto completing my input field and need 4 events:
- v-on:click="addReceiver"(on button 'add') - done
- v-on:enter="addReceiver" (on input field) - done
- click on list item - done
- enter on list item - done
For now my code (without unnecessary things) looks like this:
export default {
data() {
return {
contacts: [],
receivers: []
}
},
mounted() {
this.fetchMessages();
},
methods: {
fetchMessages() {
axios.get('api/messages').then(response => {
this.contacts = response.data.contacts;
var options = {
data: this.contacts,
getValue: "name",
list: {
match: {
enabled: true
},
onClickEvent: function() {
var name = $("#to").getSelectedItemData().name;
$("#list_of_receivers").append("<tr><td>"+name+"</td></tr>");
//this.receivers.push(name); CANT DO THAT ALSO!!!
},
onKeyEnterEvent: function() {
var name = $("#to").getSelectedItemData().name;
$("#list_of_receivers").append("<tr><td>"+name+"</td></tr>");
}
}
};
$("#to").easyAutocomplete(options);
});
},
addReceiver(){
var name = $("#to").val();
$("#list_of_receivers").append("<tr><td>"+name+"</td></tr>");
}
}
}
As you can see I need to duplicate my code in many places, because I cant use function addReceiver() inside for example onClickEvent: jquery list function.
Thank you for any help!
addReceiverand use this reference to declare your other functions like thisonClickEvent: yourNamedFunction,andaddReceiver(){ yourNamedFunction(); }. References to objects like$can be inside, as long as it's defined when the function is called, it should be OK