user1578653
user1578653

Reputation: 5028

ExtJS 4.2 - have a remote-loading store that appends records rather than replaces them?

I have a store which I want to hardcode some initial values into. Then when it loads from the server I want it to APPEND the results to the initial values, rather than replacing them. Is this possible?

This is what I have at the moment:

var myStore = Ext.create("Ext.data.Store",{
  autoLoad: true,
  fields:[
    {name:"id", type:"int"},
    {name:"username", type:"string"}
  ],
  data: [
    {id: 1, name: "User1"},
    {id: 2, name: "User2"}
  ],
  proxy: {
    type: 'ajax',
    url: 'users.php?method=getUsers',
    reader: {
      type: 'json',
      root: 'results'
    }
  }
});

I want the data that gets returned from the PHP script to be added to the "User1" and "User2" already in the store.

Upvotes: 2

Views: 1904

Answers (2)

Mike
Mike

Reputation: 1013

Try this one:

store.load({
    addRecords: true
})

Upvotes: 6

user1578653
user1578653

Reputation: 5028

The solution I got to in the end was to install a listener on the load event of the store, and add the default data manually each time the store reloads:

var myStore = Ext.create("Ext.data.Store",{
  autoLoad: true,
  fields:[
    {name:"id", type:"int"},
    {name:"username", type:"string"}
  ],
  proxy: {
    type: 'ajax',
    url: 'users.php?method=getUsers',
    reader: {
      type: 'json',
      root: 'results'
    }
  },
  listeners: {
    load: function(){
      var me = this;
      me.add({id: 1, name: 'User1'});
      me.add({id: 2, name: 'User2'});
    }
  }
});

This still doesn't solve the problem of how to get the store to append data each time, but luckily this solution is OK for me!

Upvotes: 0

Related Questions