Delete object in array in Riot.js when i have multiple tags

130 Views Asked by At

I try to delete an object from an array but I can't access the array for some reason. How can I get the array object in the delete_task function?

In task.tag.html

<task>
    <button onclick={opts.delete_task}>{ title }</button>
</task>

In tasklist.tag.html

<tasklist>
    <todo title="test" delete_task={delete_task}></todo>

    <script>
        this.delete_task = function () {
          for (var i = tasks.length - 1; i >= 0; i--) {
           if (tasks[i].title === this.item.title) {
              tasks.splice(i, 1);
            }
          }
        };

        this.tasks = [{title: "test"}]
     </script>
</tasklist>
1

There are 1 best solutions below

0
On

You can use parent word, according to the nested loops approach

And the item from the event, according to the events handling

In the result you could have something like that: https://jsfiddle.net/relaximus/q4t8e0sc/

Actually, as the click event is bubbling up, you can use onclick on the whole task tag, in this case you will not need to pass the delete handler as a function in the tag.

And just write

 onclick={ parent.deleteTask }