0

I am trying to build a store that will generate a model based on data from the server Eg in the form of:

{success:true,data:[item1:{},item2{}...]}

Is there any way I can over write the model of a store?

This is what I have atm:

Ext.define('Tan.data.SimpleStore', {
extend: 'Ext.data.Store',
constructor: function (config) { //Not initComponent, thx for wasting an hour...
    config=this.config||{};
    Ext.define('Users', { //ext base example model.
        extend: 'Ext.data.Model',
        fields: [
            {name: 'id', type: 'string'},
        ]
    });
    config.proxy= { 
        type: 'ajax',
        url : 'server.php?c=testFunction',
        reader: {
            type: 'json',
            root: 'data'
    }//Example return: {"success":true,"data":["0":1,"2":1,"3":1,"4":1]}

    }
    config.model='Users';//Store breaks without this :((
    this.callParent([config]); //Proto It
    this.load({ // watch for the load event and then manipulate
                    // ( or try to atleast ) the fields
        callback: function(records,operation,success){
 // As expected obj is correct
            var obj=Ext.decode(operation.response.responseText);     
 //K now wtf. Have the data but....
            Ext.ModelManager.unregister(this.config.model);

            Ext.define(this.config.model, {
                extend: 'Ext.data.Model'
                ,fields: Ext.Object.getKeys(obj.data)
                ,url : 'server.php?c=testFunction'
                ,reader: {
                    type: 'json',
                    root: 'data'
                }
            });//yay you over wrote the model, but meh, nothing
            console.log(this); //Bleh 0 items in store.data
        }
    });
}
});

var s= Ext.create('Tan.data.SimpleStore');
s.load();

It appears that one must have a model declared when loading a store, so I just declare a crud one, with every intention of over writing it.

Theoretically I suppose it might work by doing a callback on an ext.Ajax call as a constructor function, but I am trying to avoid it.

Alex
  • 5,674
  • 7
  • 42
  • 65

3 Answers3

0

You can define your own reader or override Reader.read method.

I've solved similar problem in this post.

Community
  • 1
  • 1
Molecular Man
  • 22,277
  • 3
  • 72
  • 89
0

This was oddly simple.

this.add({key:'value}); 

Registers the field.

Ext.define('Tan.data.SimpleStore', {
extend: 'Ext.data.Store',
constructor: function (config) {
    config=this.config||{};
    Ext.define('Users', {
        extend: 'Ext.data.Model',
        fields: [
        ]
    });
    config.proxy= {
        type: 'ajax',
        url : 'server.php',
        reader: {
            type: 'json',
            root: 'users'
    }
    }
    config.model='Users';
    this.callParent([config]);
    this.load({
        callback: function(records,operation,success){
            var obj=Ext.decode(operation.response.responseText);     
            this.add({data:obj}) //this is the magic
            console.log(this.getRange());
              console.log(this.max('data'));//yay it logs my array, looks like the handlers are in place
        }
    });
}
});

var s=   Ext.create('Tan.data.SimpleStore');
s.load();
Alex
  • 5,674
  • 7
  • 42
  • 65
-1

I've asked a similar question in Sencha's forums, but I haven't got a response.

Sorry I'm not giving you a direct answer.

pablodcar
  • 798
  • 1
  • 6
  • 15
  • Best results I've had with charts so far is to recreate the entire object based on a store onload event. Bit hacky, but it allowed for more custimizability ( eg dynamic min / max etc ). Changing the config of the graph ( even to the extent of redifining a store & model ), shouldnt be that painful that way. – Alex Jul 22 '11 at 13:53