Search code examples
extjsextjs3

Custom store implementing errors


I'm trying to implement custom store with ExtJS 3.4. I use this forum post with ExtJS4 version.

Now my code looks like this:

Ext.define('TestStore', {
    extend: 'Ext.data.Store',
    //model: 'TestModel',
    fields: [
        {name: 'date'},
        {name: 'number'},
        {name: 'percent'}
    ],
    storeId: 'TestStore',
    generateData: function() {
        var me = this,
            data = [];
        // generate 10 records
        for( var i=0;i<10;i++) {
            data.push([
                me.randomDate(new Date(2012, 0, 1), new Date()),
                Math.floor( Math.random() * 1000 ),
                ( ( Math.random() * 1000 ) / 3.2 ).toFixed( 1 )
            ]);
        }
        console.log(data);
        return data;
    },
    randomDate: function(start, end) {
        return new Date(
            start.getTime() + Math.random() * (end.getTime() - start.getTime())
        );
    },
    constructor: function() {
        console.log('constructor!');
        var me = this;
        me.superclass.constructor.apply(me, arguments);
        me.loadData(me.generateData(), true);
        //me.add(me.generateData());
    }
});

And I see this error:

TypeError: this.reader is undefined

Part where my code breaks:

loadData : function(o, append){
    var r = this.reader.readRecords(o); <-------
    this.loadRecords(r, {add: append}, true);
},

How to correctly set reader? I think I need ArrayReader, but I can't set it properly. I tried so much ways...


Solution

  • Working code for ExtJS 3.4:

    App.store.documents.documentsRandomStore = Ext.extend(Ext.data.Store, {
    reader: new Ext.data.ArrayReader(
        {
            idIndex: 0  // id for each record will be the first element
        },
        //rt // recordType
        Ext.data.Record.create([
            {name: 'date'},
            {name: 'number'},
            {name: 'percent'}
        ])
    ),
    storeId: 'documentsRandomStore',
    generateData: function(count) {
        var data = [];
        // generate records
        for(var i=0; i<count; i++) {
            data.push([
                this.randomDate(new Date(2016, 0, 1), new Date()),
                'Документ ' + Math.floor( Math.random() * 100 ),
                ( ( Math.random() * 1000 ) / 3.2 ).toFixed( 1 )
            ]);
        }
        return data;
    },
    randomDate: function(start, end) {
        return new Date(
            start.getTime() + Math.random() * (end.getTime() - start.getTime())
        );
    },
    constructor: function(count) {
        App.store.documents.documentsRandomStore.superclass.constructor.apply(this, arguments);
        this.loadData(this.generateData(count));
    }
    });