开发者

Ext.app.Application does not create global variable using name config

开发者 https://www.devze.com 2023-03-23 19:51 出处:网络
In the 4.0.2a docs: http://docs.sencha.com/ext-js/4-0/#/...pp.Application I see this: Ext.application({

In the 4.0.2a docs: http://docs.sencha.com/ext-js/4-0/#/...pp.Application

I see this:

Ext.application({
    name: 'MyApp',
    launch: function() {
        Ext.create('Ext.container.Viewport', {
            items: {
                html: 'My App'
            }
        });
    }
});

"This does several things. First it creates a global variable called 'MyApp' - all of your Application's classes (such as its Models, Views and Controllers) will reside under this single namespace, which drastically lowers the chances of colliding global variables."

When I run this code, I do not see a global variable called MyApp... does anybody else have this problem?

Here is my entire app (in a single HTML page):

<!DOCTYPE html>
<html>
<head>
    <title>Testing ExtJS 4</title>
    <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" />
    <script type="text/javascript" src="extjs/ext-all-debug-w-comments.js"></script>
    <script type="text/javascript">
    Ext.application({
        name: 'MyApp',
        launch: function() {
            Ext.create('Ext.container.Viewport', {
                items: {
                    html: 开发者_运维问答'My App'
                }
            });
        }
    });
    Ext.onReady(function() {
        alert(typeof MyApp);
    });
    </script>
</head>
<body></body>
</html>


it's not working because as the api states (guide/mvc application architecture):

"... All Ext JS 4 applications should only use a single global variable, with all of the application's classes nested inside it...".

If you try with this code:

 Ext.application({
    name: 'MyApp',
    appFolder: '/app',

    autoCreateViewport: true,

    launch: function() {
        console.log(MyApp);

    }
});

you will see that the global variable exists. You don't need to access the application from any other place than the application itself


I get the same error, and here are what I found.

In index.html:

Ext.Loader.setConfig({enabled:true});

Ext.application({
    name: 'MyApp',
    controllers: ['Test'],
    launch: function() {
        Ext.create('Ext.container.Viewport', {
            items: {
                html: 'My App'
            }
        });
    }
});

Ext.onReady(function() {
    alert(typeof MyApp);
});

Create a small controller: app/controller/Test.js (/app has the same parent folder as /extjs)

Ext.define('MyApp.controller.Test', {
    extend: 'Ext.app.Controller',

    init: function() {
        console.log('The controller was initialised');
    }
});

When running, in Firebug you will see the MyApp global variable. However, a messagebox of 'undefined' still appeared for alert statement.

IMHO MyApp is an object to "maintains references to all of the models, views and controllers used by the app". Maybe extjs use some type of class "Dynamic loading"; so it will not create that global variable until there is something (controllers, views, models) to contain inside (I am not sure of this). In this case the variable must be created to contain the controller Test. However, I cannot explain the 'undefined' message for alert. Maybe at that time the Ext object is ready but "Dynamic loading" to create the viewport and the variable MyApp is not finished (not sure).

Hope this help.

0

精彩评论

暂无评论...
验证码 换一张
取 消