Singleton in ExtJs4

I want to create a singleton object in Ext JS 4.

Here is my code:

Ext.define('AM.model.Test', { extend : 'Ext.util.Observable', singleton : true, foo : function() { console.log('log 1'); }, constructor : function() { } }); 

I call it with

 AM.model.Test.foo(); 

Everything works fine while I set the definition in app.js. When I try to move this definition to "app \ model \ Test.js", I get this error:

AM.model.Test is undefined [Break On This Error]
AM.model.Test.foo();

What is wrong with my code?

+6
source share
1 answer

You need to configure dynamic loading and require the class from which you are going to use it.

For instance:

 Ext.Loader.setConfig({ enabled: true, paths: { 'AM': 'app' } }); Ext.require('AM.model.Test'); Ext.onReady(function() { // now all the required classes are loaded and you can start using them AM.model.Test.foo(); }); 
+5
source

Source: https://habr.com/ru/post/910679/


All Articles