Ext 4.1 - Accessing a class instance using Ext.define()

In my code, I have a class with a method defined like this:

Ext.define('MY.class.Manager', {
    ....
    ....
    ....
    manageStuff: function(){


    }
}

Initially, the manageStuff function was only needed in one place and everything worked fine. But now, I've added another class where I need to call the manageStuff function from within it.

I attempted to obtain a reference in the new class:

var ManagerClass = Ext.getClass('MY.class.Manager');

As well as:

var ManagerClass = Ext.getClass('class.Manager');

However, both of these attempts returned null.

To confirm that the Manager Class is being defined before attempting to retrieve it, I inserted print statements:

These statements showed:

...making manager class
...getting manager class

I am seeking assistance because I believe my approach to this issue may be incorrect from the start.

Answer №1

If you want to share behavioral traits among classes that are not related, consider using mixins:

Ext.define('MyApp.mixin.Foo', {
    foo: function(bar, baz) {
        alert(bar + ' ' + baz);
    }
});

Ext.define('MyApp.class.Foo', {
    mixins: [
        'MyApp.mixin.Foo'
    ],

    methodThatCallsFoo: function(bar, baz) {
        this.foo(bar, baz); // The foo method has been mixed in
    }
});

This practice is applicable to Ext JS 4.x and beyond; it's worth noting that in Ext JS 5, this feature became even more robust. For more information, refer to the Ext.Mixin documentation.

Answer №2

To make the method accessible globally for both classes, you can create a global namespace using Ext's Ext.namespace. In this instance, let's define MY as the namespace to contain all global variables and methods, with MY.helper specifically for helper methods like manageStuff.

Ext.namespace(
    'MY',
    'MY.helper',
    // add more namespaces inside 'MY' if needed ...
);

Next, declare your helper method:

MY.helper.manageStuff = function() {
    // perform necessary actions...
};

This method (along with any other data stored in MY) can now be accessed anywhere within your application.

Ext.define('MY.class.Manager', {
    //...
    //...

    someFunctionThatCallsManageStuff: function(){
        MY.helper.managerStuff();
    }
});

Please note that in JavaScript, you can directly store variables in the global namespace, but it may cause clutter. Alternatively, use a single variable like MY to organize global variables. For instance, you can create "packages" under MY like MY.helper.

Also, the use of Ext.namespace here is similar to manually initializing MY as a global variable and adding properties to it:

//initialize MY as a global variable
MY = {};
// add properties to MY
MY.helper = {};
// add properties to MY.helper
MY.helper.manageStuff = function() {
    // perform necessary actions...
};

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

How can I retrieve data from a script tag in an ASP.NET MVC application?

I'm struggling to figure out how to properly access parameters in a jQuery call. Here is what I currently have: // Controller code public ActionResult Offer() { ... ViewData["max"] = max; ViewData["min"] = min; ... return View(paginatedOffers ...

Delay the loading of JavaScript libraries and multiple functions that are only executed once the document is

After learning how to defer the loading of JS libraries and a document ready function from this post, I encountered a challenge. The code I currently have handles multiple document ready functions inserted by different modules, not on every page. echo&ap ...

What is the reason that the 'mouseenter' event only applies to the initial element in each round of iteration within a spacebar loop?

My goal is to create an off-canvas menu within a template component. I found inspiration from this helpful article. The setup I have is quite common: A container tab where I loop through an items collection An item component that contains the off-canvas ...

There is no 'depto_modules.length' property in this row. How should I go about fixing this issue?

I have a table set up to display data from an associated table. The functionality is working fine, but I keep seeing a warning message when I apply certain filters: The warning states that the property depto_modules.length does not exist in the row. It ad ...

Preventing Content Jumping When Concealing Elements: Tips and Tricks

I've been working on a project at , where you can click on a small map to expand a larger map below it. However, I noticed that when you scroll down to the large map and then try to close it using the button at the bottom, the content on the page jum ...

Guide on retrieving just the time from an ISO date format using JavaScript

let isoDate = '2018-01-01T18:00:00Z'; My goal is to extract the time of 18:00 from the given ISO date using any available method, including moment.js. ...

Preventing multiple clicks by toggling the HTML tag on and off

Here is the jQuery structure that I am currently working with: $(document).on('click', '.view-details', function () { $(this).prop("disabled", true); // API call1 // API call2 // API call3 $(this).prop("disabled" ...

When invoking a function that has been returned, the error message "invalid function" is displayed

I've been working on a wordle-style game, but I'm running into an issue with fetching the API and getting the validation functions to work properly. Every time I press submit, I keep getting an error saying "getData(...).isCorrect is not a functi ...

Creating a map in Typescript initialized with a JSON object

In my Typescript class, there is a map that I'm trying to initialize: public map:Map<string,string>; constructor() { let jsonString = { "peureo" : "dsdlsdksd" }; this.map = jsonString; } The issue I'm encounte ...

Check the feature that retrieves data from a `json` file

In my util file, I have a function that imports and checks whether a given sectionUUID has a video in the JSON file. import multipleVideos from '../data/videos.json' function hasSectionMultipleVideos (sectionUUID) { return multipleVideos.vide ...

Updating row color according to values obtained from the map function in ReactJs

I have been experimenting with various methods to change the color of table rows based on specific values within a map function. Despite trying solutions like the UseRef hook and browsing through stack overflow, I have yet to achieve success. {dat ...

Turn off client-side hydration in Nuxt.js or Prevent leaking raw data in Nuxt.js

Working on a Web App built with Nuxt.js for Server-Side Rendering poses some challenges. To safeguard my backend data, I turned to asyncData and Axios for communication with the server. However, Nuxt.js inadvertently exposed my backend data to clients th ...

Modify the color of chips when the checkbox is toggled in Materialize CSS

I have been scouring the internet for a solution to my issue. Currently, I am working with materialize css chips and I am trying to achieve a specific functionality. I want the color of the last chip to turn green and the rest to turn red when a checkbox ...

Exploring ways to showcase informational alerts when a link is hovered over by the mouse

I am currently working on a website that showcases links utilized by my team. One specific requirement is that when a user hovers over a link, note information should be displayed. Although the simplest solution would be to not list the link if it's n ...

What could be the reason behind the button's lack of color change with this particular code?

I'm a beginner in web development and currently practicing HTML, CSS, and Javascript. I've added a button to my html file and would like the color of the button to change when it's clicked on. Here is my HTML code: <button id="box&q ...

While running tests on a React project, the `npm test` command is successful, but unfortunately,

I created a new react app using create-react-app and included some basic components and tests. The tests work fine when running 'npm test', but I encounter an 'Unexpected token' error when using Jest to run the tests with imported compo ...

Events in EmberJS that occur after the content has been modified

Need assistance with implementing an alert event for a new tab added to the default ones. Solution: Develop a TabsController Create an initilizerView which uses a list parameter to manage the TabsController.Content Upon insertion of the view, add the ac ...

How to filter dependency-injected asynchronous data based on conditions?

I have developed a NodeJS application that utilizes dependency injection. One of the key features of the app is that it can execute multiple functions (modules) simultaneously, and if multiple modules request data from the same async resource, the app ensu ...

Is there a way to update the Angular component tag after it has been rendered?

Imagine we have a component in Angular with the selector "grid". @Component({ selector: 'grid', template: '<div>This is a grid.</div>', styleUrls: ['./grid.component.scss'] }) Now, when we include this gri ...

The absence of responseJSON in the jquery ajax response is causing an issue

Currently, I am developing a small web framework for conducting an HCI study and have encountered the following issue: In my setup, I have a Node server running with Express to serve local host data from JSON files. While it may not be the most advanced d ...