Search code examples
javascriptextjsextjs4extjs5

Adding headers to store's proxy once in ExtJS


I have a store and I need to add some headers to its proxy. I don't want to use defaultHeaders of the Ext.Ajax singleton as these are specific only to a few stores.

The headers use key / value pairs and the value comes from a variable that is NOT loaded when the store is initially loaded, the variable is populated after a successful login.

For this reason I couldn't use a constructor on the proxy or store as the variable I use for value of a header isn't available.

The only way I could get it to work was using the beforeLoad of store. Is this really the best way of achieving this ?

Here's my listener on my store, I am checking if undefined as it's fired every single time.

listeners: {
    beforeload: function( store, operation, eOpts ) {
        if (this.proxy.headers === undefined) {
            this.proxy.headers = {
                'X-GType': CompA.Items.getGtype().get('type'),

Does anyone know a better way ?

There doesn't seem to be an event that fires only once.


Solution

  • As a general answer to the problem of "events only firing once" - you can configure your listener to automatically unlisten after one event.

    listeners: { 'beforeload': { fn: function(...), single: true } }