Access to http status code from Ext.data.Store
Asked Answered
O

3

5

I have an http API which (shockingly new technique) reacts on different errors setting different response statuses.

The question is - while using Ext.data.Store with some XMLHttpRequest-inside proxy, what is the best way to handle this statuses? As far as I can understand, "load" event does not pass status directly, as well as "exception", and the last one actually doesn't even trigger when 4** status is received.

So, as I can see from code xhr instance is hidden from Ext.data.store, so the question is also can be stated as "What is best extjs practice to handle low-level xhr object".

Onia answered 21/9, 2011 at 8:38 Comment(0)
U
8

There is no exception event on Ext.data.Store. Instead, it is Ext.data.proxy.Server and its sub-classes (like Ext.data.proxy.Ajax) that define an exception event. Listeners receive a response object which includes the http status.

Depending on your setup, you can register a listener on the store's proxy, or - if your store uses a model - on the model's proxy.

This test setup worked for me on Chrome 14 and FF 6:

var store = Ext.create('Ext.data.Store', {
    fields: [ 'field1', 'field2'],

    proxy: {
        type: 'ajax',
        url: 'api/data.json',
        reader: {
            type: 'json',
            root: 'data'
        },
        listeners: {
            exception: function(proxy, exception, operation) {
                console.log(response.status);
            }

        }
    },
});
store.load(); 
Upmost answered 22/9, 2011 at 4:39 Comment(8)
downvoted - if you are talking about exception event - and I guess yoga are talking - you've just haven't read the question.Onia
Maybe you haven't read the answers ;) Fact is that in Ext 4.0.2a the proxy fires an exception event that is also triggered in case of the server returning a 4xx http status code. I updated my answer, maybe it is more of a help for you now.Upmost
exception is triggered with following statuses - 12002, 12029, 12030, 12031, 12152, 13030 - see parseStatus function. If I'm not correct, correct me, please. I've upvoted you, if your answer appears to be correct, I'll mark it as the right one.Onia
I understand better now what you are dealing with. However, 5-digit HTTP status codes are not exactly RFC 2616 compliant (this is some MS craziness, right?). If you already had a look at Ext.data.Connection#onComplete and #parseStatus then you'll know that these special 5-digit codes are not passed through to the response.status property (for whatever reasons!?).Upmost
Your question should be updated to explicitly mention those special non-compliant HTTP status codes. Also, you should re-evaluate your statement that says: '...as well as "exception", and the last one actually doesn't even trigger when 4** status is received', which is IMHO not true for Ext 4.0.2a. I am also going to update my question again to outline a possible solution to your problem.Upmost
we don't understand each other, to be honest))) My question is about standard status code fetching. As far as I have understand, there's no way to track http statuses listening some event in Evt.data.Store. If there is, please correct me.Onia
OK, misunderstanding, again. I thought that in comment #3 you state that your server responds with those (5-digit) codes. However, if I understand you correctly now, what you meant was that your interpretation of the Ext code is that the 'exception is triggered [only] with the following statuses ..'. This is not correct. If you look at Connection#onComplete you can see that the relevant IF expression evaluates the success property, not the isException property. You could have easily verified that by running the test-case I provided which I assume you didn't bother to do.Upmost
Just to clarify. The exception event on the proxy will fire in exactly the same situation as the requestexception event on Ext.Ajax. That is if either the success property of the response is false, or the HTTP status code is other than 2xx. Since registering a listener on Ext.Ajax will fire for every request made (and you commented on suknic' answer that you want it specific to your store), I offered the solution to register a listener on the store's proxy. This will fire only for exceptions triggered by requests from this store.Upmost
W
2

The exception event does provide a response object which has a status property containing the HTML status code you want to see.

If your exception is indeed not fired by 4** errors (which in my experience do fire) you could try to register an ajax listener instead:

Ext.Ajax.on('requestexception', exceptionHandlerMethod);

and

function exceptionHandlerMethod(connection, response, requestOptions, listenerOptions) {
    if(response.status == 401) {
        alert('401 error');
    }
}
Wessel answered 21/9, 2011 at 9:40 Comment(2)
thank you for this answer. As far as I can understand, Ext.Ajax is an Ext.data.Connection instance. But how I can access specific connection in Ext.data.Store? I want to use Ext.data.Store and handle somehow some response-dependant states.Onia
well, now I've understand the underlying ideology better. Thanks a lot.Onia
S
0

Using extjs 5.1

The store/proxy can access every response http code & header by listening to 'beginprocessresponse' or 'endprocessresponse' events.

proxy: {
    // snip

    listeners: {
        beginprocessresponse: 'onResponse'
    }

    onResponse: function(store, response, operation) {
         console.log('prior to reader');
         console.log(response.getAllResponseHeaders());
    }
}

I am curious how EventDomain plays into the mix between Ext.Ajax, Proxy, Reader, and Store.

Syllogism answered 10/3, 2015 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.