Martin
Martin

Reputation: 24318

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.

Upvotes: 0

Views: 1098

Answers (2)

Robert Watkins
Robert Watkins

Reputation: 2206

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 } }

Upvotes: 1

bulforce
bulforce

Reputation: 1071

In general it will be better if you create your view after the 'critical' store is loaded. Meaning that you should most likely need to set autoCreateViewport to false in your application main file. Then manually create it in the store success call back. Hook in the init() method to do that.

Example: (pseudo code)

App.js

init: function() {
            myImportantStore.load({
               success: function() {
                 // create your viewport
               }
           });
}

Upvotes: 0

Related Questions