Alex
Alex

Reputation: 5724

Passing config to ExtJS prototypes

I'm trying to figure out how ExtJS4 passes around config objects. I want to do the equivalent of...

store  = function(config){
    if ( typeof config.call !== 'unndefined' ){
        config.url = "server.php?c=" + config.call || config.url;       
    };  
    Sketch.Data.AutoSaveStore.superclass.constructor.call(this,config);
};
Ext.extend(store, Ext.data.Store{})    

I am probably missing something obvious here, but having dug around in the sandbox file, the closest I have come is....

 Ext.define('My.awesome.Class', {
     // what i would like to pass.
     config:{},
     constructor: function(config) {
         this.initConfig(config);
         return this;
     }
 });

which doesn't seem to work if you do something like...

var awesome = Ext.create('My.awesome.Class',{
    name="Super awesome"
});  
alert(awesome.getName()); // 'awesome.getName is not a function'

However

  Ext.define('My.awesome.Class', {
     // The default config
     config: {
         name: 'Awesome',
         isAwesome: true
     },
     constructor: function(config) {
         this.initConfig(config);
         return this;
     }
 });

var awesome = Ext.create('My.awesome.Class',{
    name="Super awesome"
});  
alert(awesome.getName()); // 'Super Awesome'

This is biting me in the rear end when trying to do complex store extensions. Anyone have any idea how I pass a bunch of random params to the prototype?

Upvotes: 0

Views: 7571

Answers (1)

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

You should not be using new operator to create new instance on your class. In ExtJS4, you should use Ext.create() method.

Try doing:

var awesome = Ext.create('My.awesome.Class');
alert(awesome.getName());

And if you want to pass some param when creating an instance, you can do the following

var awesome = Ext.create('My.awesome.Class',{name:'New Awesome'});

Upvotes: 5

Related Questions