I am trying to build an array of objects in VueJS and I am having problems with .$setand .$add.
For example, when adding new elements / objects, I need the following structure:
{
"attendees": {
"a32iaaidASDI": {
"name": "Jane Doe",
"userToken": "a32iaaidASDI",
"agencies": [
{
"name": "Foo Co"
}
]
}
}
}
New objects are added in response to an AJAX call that returns JSON in the same format as above. Here is my Vue instance:
var vm = new Vue({
el: '#trainingContainer',
data: {
attending: false,
attendees: {}
},
methods: {
setParticipantAttending: function(data)
{
if (data.attending)
{
this.attendees.$add(data.userToken, data);
} else {
this.attendees.$delete(data.userToken);
}
}
}
});
This only works if I start with attendees: {}in mine data, but when I try attendees.lengthafter adding a member, I get undefined. If I use attendees: [], a new object is not added. And finally, if I use .$set(data.userToken, data), it is not added to the required format 'token':{data..}.
What could be the problem? What is the correct way to use $.addwhen starting with an empty array of objects?
UPDATE
, , attendees: {}, , ,
if (data.userToken in this.attendees) {
this.attendees.$set(data.userToken, data);
} else {
this.attendees.$add(data.userToken, data);
}
, .