PDA

View Full Version : How do I update a store?



profunctional
27 Oct 2010, 8:24 PM
I have a store that is loaded by an ajax request. In my app, I pull one of the records in the store and update it. How do I update the store to reflect this change?

evant
27 Oct 2010, 8:47 PM
Use the set method on the model.

profunctional
27 Oct 2010, 9:20 PM
Is there anyway to just remove the record from the store and insert my new item into the store? Of course, the store would need to be resorted etc for the List.

If I add a new record, I will also need to insert this into the store and have the list resorted.

I am trying to avoid a trip to the server to reload the list. Is this possible?

evant
27 Oct 2010, 9:21 PM
There's methods for that as well, add/remove on the store. Look through the API docs, all the info you need is there.

profunctional
27 Oct 2010, 9:28 PM
I've already been through the docs and it suggests i do something like:



var existingTicket = ticketListStore.getById(x.TicketId);

if (existingTicket != null)
{
var index = ticketListStore.indexOf(existingTicket);
if (index != -1)
{
ticketListStore.removeAt(index);
ticketListStore.insert(index, x);
}
}
ticketList.refresh();


Only problem is that the insert fails. it throws an error that there is no method set.

If I add instead of insert like this : ticketListStore.add(x); It works. But obviously it adds it to the bottom which is wrong.

evant
27 Oct 2010, 9:30 PM
What is "x"?



Ext.setup({
onReady: function() {
Ext.regModel('Person', {
fields: ['first', 'last']
});

var s = new Ext.data.Store({
model: 'Person',
data: [{
first: 'Evan',
last: 'Trimboli'
},{
first: 'Aaron',
last: 'Conran'
},{
first: 'Ed',
last: 'Spencer'
}]
});

s.removeAt(0);
s.insert(1, Ext.ModelMgr.create({
first: 'Evan',
last: 'Trimboli'
}, 'Person'));

s.each(function(m){
console.log(m.get('first'), m.get('last'));
});
}
});

profunctional
27 Oct 2010, 9:38 PM
[QUOTE=evant;531192]What is "x"?

x = Ext.decode(response.responseText);

profunctional
27 Oct 2010, 9:42 PM
With your sample, the list is not sorted the way I set it up in this code:


var ticketListStore = new Ext.data.Store({
model: 'TicketModel',
sorters: 'TicketStatus',
getGroupString : function(record) {
return record.get('TicketStatus');
}
});



What is "x"?



Ext.setup({
onReady: function() {
Ext.regModel('Person', {
fields: ['first', 'last']
});

var s = new Ext.data.Store({
model: 'Person',
data: [{
first: 'Evan',
last: 'Trimboli'
},{
first: 'Aaron',
last: 'Conran'
},{
first: 'Ed',
last: 'Spencer'
}]
});

s.removeAt(0);
s.insert(1, Ext.ModelMgr.create({
first: 'Evan',
last: 'Trimboli'
}, 'Person'));

s.each(function(m){
console.log(m.get('first'), m.get('last'));
});
}
});

evant
27 Oct 2010, 9:46 PM
Why would it? You're explicitly inserting it at a particular index.

profunctional
28 Oct 2010, 5:29 AM
You misunderstand me. My attempt to insert at an index is for editing a record. If I were to add a new record into the store, I would want it sorted.