Tips for receiving updates when a property within one object changes using rxjs

Exploring the world of Angular and RxJS is a new adventure for me. Currently, I am working on developing an Angular component that facilitates multiple objects sharing a tree-structured dataset.

Let's dive into my scenario in a fictional yet simplified manner - creating an Angular component to transmit an entire company structure to the server. This structure comprises various "StuffDto" objects that reference each other as their managers, while all staff members share a common departmental hierarchy map within the company.

The intricate multi-level hierarchy map resembles:

  • Marketing Dept
    • Sales Dept
      • In-store Sales Team
      • On-site Sales Team
    • Advertisement Dept
      • Planner Team
      • Designer Team
  • Manufacturing Dept
    • R&D Dept
      • Engineer Team
      • Researcher Team
    • QA Dept
      • Inspector Team
      • Assurance Team

Presenting the StaffDto class which will undergo direct submission to the server upon completion.

export class StaffDto {
    id: string;
    name: string;
    assignedDept?: string;
    managerId?: string;
}

Introducing the Stuff object that will circulate through my Angular applications.

export class Stuff {
    data: StuffDto;
    _manager: Stuff;

    get manager(): Stuff {
        return this._manager;
    }

    set manager(manager: Stuff) {
        this._manager = manager;
        this.data.managerId = manager.data.id;
    }
}

This setup functions seamlessly in certain scenarios, however not in all cases.

Suppose I create Peter and Tom, then designate Peter as Tom's manager. Below are the representations of the 2 StuffDto objects.

{ id: abc01,
  name: 'Peter',
  assignedDept: 'Marketing Dept',
  managerId: null }
{ id: abc02,
  name: 'Tom',
  assignedDept: 'Sales Dept',
  managerId: 'abc01' }

If I modify Peter's id from "abc01" to "abc01a", I anticipate Tom's ManagerId to automatically update as well. Similarly, alternation of Peter's assignedDept from "Marketing Dept" to "R&D Dept" should result in nullifying Tom's assignedDept, as there exists no "Sales Dept" beneath Peter's new department.

I am uncertain if this can be achieved through RxJS subscription. Though I attempted converting _manager into a Subject and subscribing to it, the function solely triggers upon assigning a new manager, neglecting changes in the manager's properties.

An inquiry lingers - is there a solution without extensive code alterations? Since the StuffDto is communicated to the server, modifications directly impacting it are out of the question.

Answer №1

If you're on the hunt for a solution, check out this article by @cartant - it might be just what you need.

The concept involves encapsulating your objects so that each property has its own observable, and using a proxy object for assignments.

I hope this meets your requirements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with const declaration in Typescript and Node.js - initializer is missing

Encountering a syntax error with the following line of code. Error message: SyntaxError: Missing initializer in const declaration const dataMap : Map<string, any> = new Map(); ...

Implement validation within the child component using Reactive Forms

In this demonstration, a reactive form has been created with an input field placed inside a child component. The goal is to add a new form control within the child component and perform validation on it within the same component. However, an error has been ...

Error: Angular 2 Application lacks 'Access-Control-Allow-Origin' header

Currently, I am working on a project where I am focusing on learning and practicing Angular 2. Since I do not have a server-side setup, I am making API requests to the barchart ondemand api. I am facing an issue with CORS and I am wondering if there is a ...

Having trouble with installing Bootstrap in Angular 5

My journey with Bootstrap began by executing the command below: npm install --save bootstrap The installation was a success, and I proceeded to incorporate the CSS as follows: "styles": [ "../node_modules/bootstrap/dist/css/bootstrap.min.css", ...

Using React-Router-Native to send an image as a parameter

I am encountering an issue while attempting to pass an image as a parameter in react-router-native and retrieve the data from location.state. Typically, I use the following code to display an image: import Icon from '../image/icon.png'; <Vie ...

In order to address the issue of displaying a 404 error in In-Memory Angular,

I have watched all the videos regarding the In-memory web API and diligently followed all the steps and instructions. However, I am still encountering a 404 Error. Please inform me if I missed something or made an error. I have attempted to troubleshoot an ...

Setting up the production environment

I'm attempting to set up my app in the production environment by executing ng serve --configuration=production However, I'm encountering the error message "An unhandled exception occurred: Project 'production' does not support the &a ...

Responding to modifications occurring beyond the Angular2 framework

I have a traditional non-angular web page built with basic JavaScript, and I am interested in integrating Angular2 for some new functionalities. My idea was to bind an Angular2 component to an object being updated by the existing JS code, allowing Angular2 ...

Creating synchronization mechanisms for events in JavaScript/TypeScript through the use of async/await and Promises

I have a complex, lengthy asynchronous process written in TypeScript/JavaScript that spans multiple libraries and functions. Once the data processing is complete, it triggers a function processComplete() to indicate its finish: processComplete(); // Signa ...

Tally up identical words without considering differences in capitalization or extra spaces

Let's take an example with different variations of the word "themselves" like "themselves", "Themselves", or " THEMSelveS " (notice the leading and trailing spaces), all should be considered as one count for themselves: 3 ...

I am receiving null values for my environment variables

Seeking assistance with my angular 12 + node 14 project. Despite researching extensively, I keep encountering an undefined error when trying to utilize environment variables. I have placed a .env file in the same folder as my login.component.ts since my r ...

Creating a standard Modal component in Angular

I am trying to create a versatile Modal component. When the user clicks on the Add button, I want the modal to open with the functionality to add new content. Similarly, when the user clicks on the Edit button, I want the same modal to display edit functio ...

When aot is enabled, ngClass and ngIf condition may not compile successfully

I am encountering an issue with a div using ngClass and ngIf conditions: <div [ngClass]="{ 'active': nbActive === 1 }" > <!-- some stuff --> </div> There is also a similar div using a ngIf condition: <div *ngIf="nbActi ...

What is the correct location to indicate the delete-output-path?

I'm currently working on a Node Express project with Angular integrated using Angular CLI, specifically created with ng new. I want to prevent the Angular output from overwriting the distribution folder. I've heard about a delete-output-path pa ...

Tips for storing an httpOnly cookie containing a JWT token within an angular 12 application

I am implementing a Single Sign-On (SSO) integration that provides a JWT token. However, I am hesitant to store this token in local storage due to security concerns. Is there a way to securely manage this token without repeatedly fetching it from the ser ...

TS does not approve of optional chaining when it comes to objects

type Object1 = { link: 'niceurl.com' } type Object2 = { source: 'differenturl.com' } function AnotherComponent(item: Object1 | Object2) { return ( <img src={item?.link || item?.source} /> ) } Output: Error: Propert ...

The express application's GET route is causing a "Cannot GET" error to be thrown

I am managing different types of pages: the main root page (/) today's chapter page (/929 OR /929/) which eventually redirects to /929/<CHAPTER> where <CHAPTER> is a natural number between 1 to 929 individual chapter pages (/929/<CHAP ...

Having trouble with Angular 2's Output/emit() function not functioning properly

Struggling to understand why I am unable to send or receive some data. The toggleNavigation() function is triggering, but unsure if the .emit() method is actually functioning as intended. My end goal is to collapse and expand the navigation menu, but for ...

Guide to passing dropdown values with the each method and input decorator in Angular 14

I'm having trouble setting the dropdown values using a foreach method in my code. It seems to be loading 5 times for each dropdown, which is not what I want. I only want to display the specific value for each dropdown option (e.g. only the "Car" value ...

Discover the files in the web directory using TypeScript

I am working on a TypeScript web application that has a specific folder structure. Here is how it looks: - assets |- a.png |- b.png |- c.png |- d.png |- ... - app.ts My question is: In the app.ts file, how can I programmatically list all the files wi ...