Search code examples
feathersjs

How to trigger "updated" event of a service from another service


I've got two services in Feathers. For the sake of example, let's call them:

  1. TodoLists
  2. TodoItems

TodoItems have a N:1 association to TodoLists, and I include the TodoItems model in a find hook of the TodoLists service.

Now - on my frontend I have a listener that listens for 'update' event on TodoLists.

What is the right way to get the 'update' event emitted for TodoLists when any TodoItem is updated?


Solution

  • Try watching on all TodoItems updates and filter by your TodoList id.

    const currentTodoList = 1;
    app.service('TodoItems').on('updated', (item) => {
      if (item.todolist === currentTodoList){
        // update ui
      }
    });
    

    If you additionally want to optimize the traffic send by the backend to only send needed data, you could use some subscribe pattern (TodoListSubscription join feathers channel in create and leave in remove).