Home > DeveloperSection > Forums > Sencha Touch - Extending Store - constructor call
ben reitman
ben reitman

Total Post:96

Points:676
Posted on    May-31-2013 1:42 AM

 Sencha Touch Sencha Touch 
Ratings:


 1 Reply(s)
 1110  View(s)
Rate this:
Hi Everyone!

I'm playing around with sencha and created a list with a store and a model:

main:

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

Ext.application({ 
viewport: {
    autoMaximize: false
},                                                                                                                                                                 
name: 'Sencha',                                                                                                                                                        
     
launch: function() {     
    Ext.create('Ext.List', {
        fullscreen: true,
        store: Ext.create('store.Test', {
        }),
        itemTpl: '{lastName}, {firstName} Age: {age}'
    });
}                                                                                          
});

store:

Ext.define('store.Test', {
extend: 'Ext.data.Store',

model: 'model.User',
data: [
    { firstName: 'Ed',    lastName: 'Spencer', age: 30 },
    { firstName: 'Tommy', lastName: 'Maintz', age: 41 },
    { firstName: 'Aaron', lastName: 'Conran', age: 24 },
    { firstName: 'Jamie', lastName: 'Avins', age: 76 }
]
});
model:

Ext.define('model.User', {
extend: 'Ext.data.Model',
fields: [
    { name: 'firstName', type: 'string' },
    { name: 'lastName', type: 'string' }
]
});
To this point everything works fine.

Then I created a second store, which has another field 'age'. What I want now is to pass a parameter 'model' to the store to be able to "switch" between different 

models. I tried something like this:

store:

Ext.define('store.Test', {
extend: 'Ext.data.Store',
myModel: null,

constructor : function(model) {
    myModel = model;
}, 

model: this.myModel,

data: [
    { firstName: 'Ed',    lastName: 'Spencer', age: 30 },
    { firstName: 'Tommy', lastName: 'Maintz', age: 41 },
    { firstName: 'Aaron', lastName: 'Conran', age: 24 },
    { firstName: 'Jamie', lastName: 'Avins', age: 76 }
]
});
and in the main file:

[...]
        store: Ext.create('store.Test', {
           model: 'model.UserWithAge'
        }),
[...]
But Google Chrome gives me:

Uncaught TypeError: Object [object Object],[object Object],[object Object],[object Object] has no method 'getRange'
Any hints what I've done wrong?

Thanks in advance! 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    June-01-2013 12:51 AM

Hi Ben!

Never mind, it was a lot easier then I thought. Instead of messing with constructors or something like that it is enough to just pass the model when creating the store as I all ready did:

main:

[...]
    store: Ext.create('store.Test', {
       model: 'model.UserWithAge'
    }),
[...]
store:

Ext.define('store.Test', {
extend: 'Ext.data.Store',

model: 'model.User', //only set in case that model is not passed when created

data: [
    { firstName: 'Ed',    lastName: 'Spencer', age: 30 },
    { firstName: 'Tommy', lastName: 'Maintz', age: 41 },
    { firstName: 'Aaron', lastName: 'Conran', age: 24 },
    { firstName: 'Jamie', lastName: 'Avins', age: 76 }
]
});

I hope, it resolve your problem. 

Don't want to miss updates? Please click the below button!

Follow MindStick