Fawar
Fawar

Reputation: 735

How to use Ext.create properly

Can't find any relevant information in the sencha documention about this question :

Is it possible to call Ext.create(...) with a parameter which does not depend on the application's name? So that if I change the app's name I don't have to rewrite that line of code?

Normally I would use Ext.create(AppName.model.MYMODEL) but that's too tied to the app's name for me.

Still need help :)

Upvotes: 1

Views: 25943

Answers (3)

Fawar
Fawar

Reputation: 735

The way to do it :

You need a controller that will in it's INIT function (before UI Loading/Initiating) do the following

APPNAME = this.getApplication().getName();

Where APPNAME is a global variable.

Then when you Ext.create something you will be able to write the following

Ext.create(APPNAME +'model.MyModel');

That way you can change you app name without having to check everywhere in your code to change every single Ext.create to the new app's name.

It also give you the ability if you are to use this.getApplication().setName() to have infinite cache storage has you get 5/10mb per AppName.

Upvotes: 1

Eric
Eric

Reputation: 7005

Create using class alias

When using Ext.define to define your class, you can provide an alias property. You've probably seen this on UI components which use aliases like widget.panel. Those aliases can be used with Ext.create.

Ext.define('MyApp.SomeClass', {
    alias: 'app.someclass',  // Independent of class name
    /* ... */
});

Ext.create('app.someclass', {
    /* ... */
});

You can set the alias on a class after it has been created by using Ext.ClassManager.setAlias.

Helper function using application name

If you don't have the option to set an alias, you could create a function that wraps Ext.create which supplies your base namespace automatically.

The problem here is that Ext.application doesn't return the application object. I'm not sure how Sencha Architect generates the application code but you may need additional overrides to allow you to retrieve the application object.

function appCreate(className, config) {
    var appName = someMethodThatGetsTheApplicationName();
    return Ext.create(appName + '.' + className, config);
};

// Example usage: Creates object of MyApp.model.MyModel
var myObj = appCreate('model.MyModel', { /* ... */ });

How to get the application name at runtime

By default, Ext JS does not retain a reference to the application object when using Ext.application, so we need an override to do it. I'm using Ext.currentApp as the property to store this object, but you can change it to whatever you'd like.

Ext.application = function (config) {
    Ext.require('Ext.app.Application');

    Ext.onReady(function () {
        Ext.currentApp = new Ext.app.Application(config);
    });
};

Now that you have this, you can access the application name by simply using Ext.currentApp.name. Or, if you'd feel more comfortable using a getter you can use the following.

Ext.app.Application.addMembers({
    getName: function () {
        return this.name;
    }
});

// Example usage:
function someMethodThatGetsTheApplicationName() {
    if (!Ext.currentApp) {
        Ext.Error.raise('Current app does not exist.');
    }
    return Ext.currentApp.getName();
}

Upvotes: 6

Amit Aviv
Amit Aviv

Reputation: 1816

You can use any class name in Ext.create there is no naming convention imposed there as long as the class was already defined. If you want Ext.create to load the correct file using Ext.loader you will need to configure the loader to conform with the naming convention you need.

Upvotes: 1

Related Questions