PDA

View Full Version : Get store from proxy



Ooypunk
20 Sep 2011, 2:11 PM
Hello,
It seems I keep having troubles finding instances of classes. Now I try to get a reference to the store that contains the proxy. This is the code:

Ext.define('TB.store.Gebruikers', {
extend: 'Ext.data.Store',
model: 'TB.model.Gebruiker',
autoLoad: true,

proxy: {
[.....]
listeners: {
'exception': function(proxy, response, operation){
console.dir(this);
// var store = this.getStore();
// store.rejectChanges();
responseText = Ext.decode(response.responseText);
Ext.MessageBox.alert('ERROR', responseText.msg);
}
}
},
[...]
rejectChanges: function(store){
store.each(function(rec){
rec.reject();
if (rec.phantom){
this.remove(rec);
}
});
}
});
What I am trying to do is:
- catch any exception
- reject all changes made to the store
- give the user a notice of what has happened

I know the method rejectChanges isn't functioning yet, but right now I can't find it at all, as I can't find the store itself. Shouldn't there be some method (in the proxy class) like 'getStore' or something?
Thanks in advance.

Ooypunk
21 Sep 2011, 9:06 AM
If my question wasn't clear, I'd like to hear so.

mankz
22 Sep 2011, 12:45 AM
proxy should not be aware of Store. To get hold of store, instead configure your proxy in the constructor:



Ext.define('TB.store.Gebruikers', {
extend: 'Ext.data.Store',
model: 'TB.model.Gebruiker',
autoLoad: true,

constructor : function(config) {
config = config || {};
var me = this;
config.proxy: {

listeners: {
'exception': function(proxy, response, operation){
console.dir(me);

responseText = Ext.decode(response.responseText);
Ext.MessageBox.alert('ERROR', responseText.msg);
}
}
};
this.callParent([config]);
},
[...]
rejectChanges: function(store){
store.each(function(rec){
rec.reject();
if (rec.phantom){
this.remove(rec);
}
});
}
});

Ooypunk
22 Sep 2011, 9:05 AM
Thanks for the answer. Some things:
- why should not proxy be aware of the store?
- I tried your code, but I get an error: "namespace is undefined". I suspect this is because some function is looking up the 'config.' as part of a namespace. But how do I tell it to leave this part alone?

Izhaki
26 Jan 2012, 5:41 AM
Thanks for the reply mank, been helpful.

Here's an alternative to your code:


Ext.define('BS.store.Users', {
extend: 'Ext.data.Store',
model: 'BS.model.User',


constructor : function(config) {
this.callParent([config]);
this.proxy.on('exception', this.onProxyException, this);
},


autoSync: true,

autoLoad: true,
proxy: {
type: 'direct',
api: {
create: Users.Create,
read: Users.Get,
update: Users.Update,
},
},

onProxyException: function(proxy, response, operation, eOpts) {
this.rejectChanges();
},

rejectChanges: function()
{
this.each(function(aRec)
{
aRec.reject();
if (aRec.phantom)
{
this.remove(aRec);
}
},
this);
}



});

cjauvin
25 Jan 2013, 7:51 AM
Thanks for this great idea! Here's how it has helped me today:

In my app, I handle the need for user "relogin" (i.e. the authentication cookie may have expired in-session, for instance) by checking the "response.status" code of every Ajax transactions (I use Flask-Login, which emits a 401 whenever authentication has failed, server-side). Once the relogin is successful, I want to execute the operation that was interrupted. With "action.Load" and "action.Submit" operations, it's easy:



Ext.override(Ext.form.action.Submit, {
failure: function(form, action) {
if (action.response.status === 401) {
login_win.show();
successful_login_callback = function() {
action.run();
}
} else {
// handle other types of errors
}
}
});



For store transactions however (grids, combos, etc), I found it harder to redo their interrupted operation, because I couldn't find a way to retrieve a store from its proxy, where the exception handler is defined (prior to version 4, there seemed to be an exception handler at the store level, but not anymore). But with your idea, my pattern (for stores) becomes:



Ext.override(Ext.data.Store, {
constructor: function(config) {
this.callParent([config]);
this.proxy.on('exception', this.onProxyException, this);
},
onProxyException: function(proxy, response, options, eOpts) {
if (response.status === 401) {
var store = this;
successful_login_callback = function() {
store.load();
}
login_win.show();
} else {
// handle other types of errors
}
}
});