Inter-class communication using TypeScript callbacks

Struggling with Typescript, I have encountered an issue while trying to send a callback from an angular controller to an angular service. Despite setting a break point at the beginning of the callback function using Chrome Dev Tools, it never gets triggered and the function fails to execute.

The scenario involves utilizing the fullCalendar jQuery control. The objective is to have the Calendar_LeftClick() method defined in the CalendarController for access to scope and other variables, while having the CalendarService handle the event click on the calendar.

The approach involves CalendarService.ts constructing the fullCalendar jQuery control. ("Omg, he should use a directive! And he's using jQuery with Angular? tsk tsk" - Yes, directives will be implemented later. Currently focused on resolving callback issues with TypeScript.)

public createCalendar(eventList): void {
    $('#calendar').fullCalendar({
        height: 'auto',
        events: eventList,
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
        },
        editable: true,
        droppable: true,
        selectable: true,
        eventClick: this.calendarEventClick
    });
}

public registerClickObserver(callback): void {
    if (this._observerCallbacks == null)
        this._observerCallbacks = [];

    this._observerCallbacks.push(callback);
}

public calendarEventClick(event, jsEvent, view): void {
    this._currentId = event.id;
    this._currentEvent = event;

    angular.forEach(this._observerCallbacks, (callback) => {
        callback(event, jsEvent, view);
    });
}

Within CalendarController.ts, the following steps were taken...

constructor(...) {
    this.CalendarService.registerClickObserver(() => this.Calendar_LeftClick);
}

public Calendar_LeftClick(event: any, jsEvent: any, view: any) {
    //...other code here
    this.Calendar_CreateTooltip(jsEvent);
}

public Calendar_CreateTooltip(jsEvent: any) {
    if (this.tooltip != null) this.tooltip.destroy();
    this.tooltip = null;
    this.tooltip = $('<div/>').qtip( "option", {
        prerender: true,
        content: {
            text: ' ',
            title: {
                button: true
            }
        },
        position: {
            my: 'bottom center',
            at: 'top center',
            target: 'mouse'
        },
        style: {
            classes: 'qtip',
            width: 300
        },
        show: false,
        hide: false
    }).qtip('api');

    this.tooltip.set({
        'content.text': (api) => {
            return this.$compile($('.tooltip-content').html())(this.$scope);
        }
    }).show(jsEvent);
}

The main goal of these actions is to display the qtip2 control (as shown in Calendar_CreateTooltip). While I had success with this setup in regular JavaScript, transitioning to TypeScript seems to have caused issues. What could possibly be going wrong?

Answer №1

When a () => this.Calendar_LeftClick callback is registered, it returns a controller method but without the necessary arguments and context since the method isn't bound. To properly call it, the registration should be written as:

this.CalendarService.registerClickObserver((...args) => this.Calendar_LeftClick(...args));

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

Utilize the jsTimezoneDetect script to showcase a three-letter time zone code

I'm currently utilizing the jsTimezoneDetect script to identify the user's current timezone. The code below shows the result as America/Chicago. Is there a way to display CDT/CST instead (based on today's date)? var timezone = jstz.determin ...

What is the purpose of the second argument in the angular module declaration?

In Angular 1.4, when I want to reference an existing module, I typically use the following code: angular.module('hotdog') From there, I can add components and factories to the module like this: angular.module('hotdog').factory(...).c ...

Preserve the variable alterations for future promises

I have implemented promises in my database access using elasticsearchjs, which utilizes Bluebird. Each ID in the list triggers a new query, and I need to identify the failing query by its ID. const idList = ['id1', 'id2', 'id3&apo ...

Adjust the image's brightness by using the value from the input range slider

I am currently utilizing fabricJS to manipulate images on a canvas. My goal is to allow users to increase or decrease the brightness of an image using a checkbox and a range selector. However, I'm encountering an issue where the image turns white when ...

Steer clear of directly modifying a prop in Vue.js to prevent errors

I have developed a custom DateField component. It is functioning properly but I am encountering an error message stating Avoid mutating the prop 'value'. This error occurs when I close the menu by clicking the Cancel button or by clicking outside ...

Configuring ng-token-auth and devise_token_auth for OAuth authentication within the Ionic InAppBrowser

After successfully testing the setup on my laptop's Chrome browser with both sameWindow and newWindow options, everything worked flawlessly. I was able to login through various platforms like Facebook, and the user authorization process went smoothly. ...

Typescript PDFjs encountering loading issues with corrupt files

In my Vue.js application, I have the following TypeScript class: /** Taken from https://github.com/VadimDez/ng2-pdf-viewer/blob/master/src/app/pdf-viewer/pdf-viewer.component.ts */ import { Component, Vue } from 'vue-property-decorator'; import ...

attach jQuery extension to several components

After spending hours scouring the internet, I have yet to find a solution to my issue. The problem is that I have developed a plugin for creating a typing effect on any given element, and it works perfectly when applied to a single element. However, when I ...

Identify the specific element that activated the MutationObserver across multiple elements

After exploring a helpful Stack Overflow post, I discovered that it is feasible to monitor multiple elements using a single MutationObserver object. In order to track the text of two specific elements on a website, I crafted the following script: var secon ...

Ways to restrict the display or height of a select HTML element

Is there a way to minimize the display or height of a select tag? Here is an image of the desired outcome: I am open to using Javascript, jQuery, CSS, or simply converting it to a list format. I just need guidance on how to achieve this or a sample to re ...

What is the process for defining a generic function to convert to an array in TypeScript?

Here is a versatile function that wraps any value into an array: const ensureArray = <T,>(value?: T | T[]): T[] => { if (Array.isArray(value)) return value if (value === undefined) return [] return [value] } const undef = undefined ensureAr ...

Trying to access the main container without also triggering the animations for its children isn't possible

Despite my extensive search efforts, I have not been able to find a solution to this seemingly common issue. As a vendor, I am limited in my ability to alter the client's HTML and use ULs. My objective is to create a hover effect where only the conten ...

Guide on utilizing direction.set within threejs for Vector3 types

In the code below, I have defined a plane, wall, and a character. Now, I am trying to set the direction using vector3(). However, I seem to be encountering an issue. Whenever I press the left or right arrow key on the keyboard, I keep receiving the follow ...

Double-executing methods in a component

I have encountered an issue while trying to filter existing Worklog objects and summarize the time spent on each one in my PeriodViewTable component. The problem I am facing involves duplicate method calls. To address this, I attempted to reset the value ...

How to Assign List Load to Controller for Google Maps?

I find myself in a predicament where I am required to retrieve the list in my Controller, but currently I am utilizing a Kendo Grid to fetch the list that will be utilized in my Google Maps. Now, my questions are: How can I directly associate the Load ...

Leveraging the useEffect hook in conjunction with the "async" keyword

Looking for a way to clean up react request in react hooks. I've heard about using AbortController in my hook, but I'm not sure how to implement it. I am currently working with next.js. What are some effective approaches to solve this issue? Also ...

Efficiency boost: Implementing ajax to load content

Let's discuss the best methods for optimizing content loading with ajax. I will outline a few techniques and provide my insights on each one. Loading html directly - This method makes it easy to load content without much additional processing requir ...

Modifying the anchor link in a pop-up window according to the clicked link using JQuery

I'm currently working on enhancing a forum where we aim to alert users when they click on links that will redirect them to an external website. Right now, my code successfully opens a pop-up window and directs the user to the intended link. However, ...

Opting to utilize a multidimensional JavaScript array or object is a more efficient method

Here is a breakdown in JSON format: jsonResponse Folder 01 Product 01 - Folder 01 Product 02 - Folder 01 Product 03 - Folder 01 Folder 02 Product 01 - Folder 02 Product 02 - Folder 02 Product 03 - Fo ...

Displaying the format when entering a value with react-number-format

How to Display Format Only After Full Value Inserted in react-number-format I recently implemented the react-number-format package for formatting phone numbers. import { PatternFormat } from 'react-number-format'; <PatternFormat value={v ...