I'm building a live notification system with Meteor, but as I've never done this before, I'm certainly tricking to get something that works.
I have a <ul>
list of notifications. I defined an event clicking on the <a class="notif-link">
that each <li>
contains.
My notifications have an ID, a type, and some other stuff we do not care about.
notifications:
[
{id: 0, type: 0, img: null, link: "/profile"},
{id: 1, type: 1, img: null, link: "/profile"},
{id: 2, type: 2, img: null, link: "/profile"},
{id: 3, type: 3, img: null, link: "/profile"},
]
...
Template.navigation.events({
"click .notif-link": function(e, tmpl){
console.log(EJSON.stringify(this)); // print the verbosed notification object as shown above
var index = Meteor.user().profile.notifications.indexOf({id: this.id});
console.log(index); // always == -1
newNotifArray = Meteor.user().profile.notifications.splice(index, 1);
Meteor.users.update({_id: Meteor.userId()}, {$set: {'profile.notifications': newNotifArray}});
}
});
The notification.id
is set as the <a>
#id
, which should allow me to identify the notification in the array and delete it when the link is clicked.
The problem is that notification.indexOf({id : this.id})
always return me -1.
I guess the problem is that I do not know how to work properly on an array of objects.
Can someone explain to me how to deal with it ?
Thanks you.