PDA

View Full Version : How to make store.removeAt() wait for server response?



wardrop
19 Jun 2013, 9:32 PM
Hi,

I'm using the removeAt() method on my store to delete records from the database. It works, but I don't like how the removal is instantly reflected in the interface, as if there's an error, it provides false assurance that the record has actually been deleted. I'd prefer for it to wait for a server response (showing some kind of loading indicator in the meantime), and only actually remove the record from the interface/store if the server returns a successful status.

Can anyone tell me how to achieve this?

Thanks

chramer
20 Jun 2013, 12:46 AM
You can try this:


var record = store.getAt(1),
silent = true;


record.destroy({
callback: function () {},
success: function () {
store.remove(record, true, silent);
},
failure: function () {}
});

The 'silent' parameter is not documented, but it exists. You can check it here (http://docs.sencha.com/extjs/4.2.1/source/Store.html#Ext-data-Store-method-remove)

wardrop
24 Jun 2013, 3:35 PM
Thanks for the help. There appears to a problem though. Including the `callback` property, even if it's an empty function, produces the error "Cannot read property 'className' of undefined", on line "ext-all-debug.js:106356". It appears as part of the process, a reference to the deleted row is trying to be obtained, which obviously fails.

Not sure if this a bug, or because of how I'm removing the record. Here's the handler for my delete button:


handler: function(grid, rowIndex) {
var store = grid.getStore()
// store.removeAt(rowIndex);
var record = store.getAt(rowIndex)
grid.setLoading(true)
record.destroy({
callback: function () { grid.setLoading(false) },
success: function () {
store.remove(record, true, true);
}
});
}

wardrop
24 Jun 2013, 4:11 PM
I get the same error with this. Not sure what's going on. ExtJS v4.2.1.883


handler: function(grid, rowIndex) {
var store = grid.getStore()
var record = store.getAt(rowIndex)
record.destroy();
}

chramer
24 Jun 2013, 10:10 PM
Your model is declared separately from your store and it has a proxy property, am I right ?

wardrop
25 Jun 2013, 3:21 PM
No, I have no model defined. All I defined was the store...



Ext.create('Ext.data.Store', {
storeId: 'people',
fields: ['id', 'name', 'age', 'town'],
proxy: {
type: 'rest',
url: '/people',
reader: {
type: 'json',
root: 'items'
},
writer: {
type: 'json'
},
listeners: {
exception: function( proxy, response, operation ){
console.log(proxy, response, operation)
}
}
},
autoSync: true,
autoLoad: true
});

chramer
25 Jun 2013, 9:52 PM
Try to define it separately.

wardrop
30 Jun 2013, 4:50 PM
This still seems like a bug. I tried with a model defined, but the same issue. Here's my all my code. It's only proof of concept stuff, but it's obviously important it works.

https://gist.github.com/Wardrop/5897712