Ways to enhance FormControl capabilities using prototypes

I am looking to add an observable or event emitter field (such as onMarkAsDirty) to all objects of the FormControl type. This field should be triggered when the markAsDirty method is called. I attempted to achieve this by extending the prototype of FormControl, but found it too challenging. Would you be able to assist me by providing some guidance or example code snippets?

Answer №1

Creating a subclass is simple. You can find the code for FormControl (actually AbstractControl) on github

Here's an example:

export class FormControlExtends extends FormControl {
  markAsDirty(opts: { onlySelf?: boolean } = {}): void {
    (this as { pristine: boolean }).pristine = false;
    
    if (this['_parent'] && !opts.onlySelf) {
      this['_parent'].markAsDirty(opts);
    }
    //do more stuff
    console.log('say hello');
  }
}

Note that to access a private variable (like "_parent" in this case), you should use square brackets instead of dot notation, so it would be this['_parent'] instead of this._parent

You can then use it like this:

control=new FormControlExtends(null)

Check out this stackblitz for a demo.

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

The destroy method of Chart.js does not appear to have any impact on the

Hello, I've come across this issue in my react app that I need help with: this.chart = new Chart(node, options); // adding data to the chart ... this.chart.destroy(); this.chart = null; this.chart = new Chart(node, options); // adding data to the cha ...

nodejs does not execute nested mysql queries

I am currently working on creating a new 'group' and assigning its creator as an administrator in the 'groupemembers' table. However, it seems that the second query is being skipped. router.post('/createGroupe/:userId', up ...

Looking to send a POST request with a particular object type

Currently, I am working with an abstract class called "Achievement" which has two subclasses: "ExhibitsVisitedAchievement" and "RouteFinishedAchievement". My goal is to create instances of these achievements by using a POST call to the relevant API endpoin ...

Creating a PDF report from a JSP page: Step-by-step guide

I'm experiencing difficulty generating the PDF report from this JSP page. While I can create the form in a .pdf format, it's not opening correctly in Adobe Reader. <% //response.setCharacterEncoding("utf-8"); //response.setHeader("Content-Tr ...

"Effortlessly emptying the text field with material-ui in React

I need assistance with clearing the content of two text fields and a button using Material-UI in React-JS. I am new to React and unsure how to achieve this. Below is the code I currently have: import React from 'react'; import RaisedButton from ...

Setting up dgrid cells to show the complete width of the information

I am developing an application that will generate a dgrid with variable column numbers and widths determined by user input. Please refer to the screenshots below for examples. The first screenshot displays a dgrid with only a few select fields, rendering n ...

Looking to access all parent elements of the <li> tag when it is clicked?

Currently, I am working on designing dynatree and the nodes are structured within ul and li tags. However, I am encountering an issue where I am unable to retrieve the parents of the clicked li element. Below is a snippet of my HTML code: <ul class="b ...

Exploring the Platform Compatibility for OneSignal Push Notifications

I'm currently utilizing Ionic to incorporate push notifications for both iOS and Android platforms. Successfully managed to implement it for iOS, however encountering difficulties initializing the configuration for Android. Whenever I input the fireb ...

Two feedbacks received from the AJAX request made to PHP backend

Looking at the code snippet below, I have a requirement for 2 specific values from the response: The HTML response retrieved from the specified page URL The value of the URL used to request the page OR the array index utilized in the call. for (le ...

Next13, varying data retrieval outcomes between server and client

Embarking on a project with Next13 has been smooth sailing so far. However, as a beginner, I am puzzled by the discrepancy in results I am obtaining from fetching data. Despite setting the component to render on the client side, it also renders on the serv ...

Having trouble interpreting JSON with Jquery

I am attempting to implement autosuggestion using Solr in conjunction with jQuery. Below is the code I have written for this purpose: $(function() { $( "#artist" ).autocomplete({ source: function( request, response ) { $.ajax({ ...

What is the process for checking pending requests within Azure Application Insights?

I successfully integrated the Azure Application Insights package into my .Net Core/Angular web application. Program.cs services.AddApplicationInsightsTelemetry(); In addition, I developed a front-end service for logging client-side events. @Injectable() ...

Extract token from the URL and then use Axios in Vue.js to send it to the POST API

Hey there, I'm in need of extracting a token from the URL http://192.168.178.25:8080/register?token=eyJhbGciOiJIUzI... and then making a POST request to the API to confirm the account. I've attempted this but encountered a SyntaxError on the ...

Encountering an issue in next.js with dynamic routes: getting a TypeError because the property 'id' of 'router.query' cannot be destructured since it is undefined

I am working on creating a dynamic page in next.js based on the ID. Here is the basic structure of my project: File path: app/shop/[id]/page.tsx This is the code snippet: "use client" .... import { useEffect, useState } from 'react' ...

Require field change in SharePoint 2010

I have implemented the following code on a SharePoint page - it locates the specified select based on its title and triggers an alert when the "Decision" value is selected. I am interested in removing the alert and instead substituting with code that iden ...

Issue with VueJS v-for when using dynamic components causing unexpected behavior

Within one of my components, the syntax I am using is as follows: <event-item v-for='(event, index) in events' :is='eventComponent(event)' :key="'event-' + index" :event='event&apos ...

Creating dynamic HTML elements in JavaScript allows you to manipulate the DOM in real time

I have a web page in HTML and I am looking to showcase a chat conversation between two individuals. The chat box structure is as follows: let data = { "lyrics": [{ "line": "line 1 start", "position": "r", "time": 4.5 }, { ...

Ways to remedy the "object may be null" issue in TypeScript without turning off strictNullChecks

Below is my typescript code snippet: const form = document.querySelector('form'); if (form != null ) { const data = new FormData(form); for (const pair of data) { console.log(pair); } // OR for (const pair of data.e ...

Unable to establish new headers once they have already been established

I am attempting to retrieve the idsFromMongo that I saved in my neo4j database and search for those ids in the mongodb to fetch the desired objects. It successfully works once, but then my server crashes and triggers the error message "Can't set heade ...

Ways to extend the parent component's prop type verification to its child components

I am facing an issue with a conditional component in my application. The component renders certain parts only if a condition is met. Sometimes, I need to pass the variable used by this conditional component to its parent component children, but TypeScript ...