SENCHA TOUCH - EXTENDING STORE - CONSTRUCTOR CALL

ben reitman

Total Post:96

Points:676
Posted by  ben reitman
 1377  View(s)
Ratings:
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! 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Sencha Touch - Extending Store - constructor call

    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. 

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!