Extjs calling function from object declaration

2.1k views Asked by At

I have this code:

Ext.define('DKM.BaseClasses.Stores.BaseStore', {
requires:['Ext.window.MessageBox'],
extend: 'Ext.data.Store',

proxy:  {
        type:'ajax',
        listeners: {
            exception: function(proxy, response, options) {                 
                requestMessageProcessor(proxy, response);
            }

        },
        afterRequest: function(request, success) {

            requestMessageProcessor(request.scope, request.operation.response);
        },
},      

requestMessageProcessor: function(proxy, response) {
...

What I would like to do is to call requestMessageProcessor. The problem (maybe a scope problem) is that I recieve an error "requestMessageProcessor is not a function".

Can anybody give me an advice?

Thanks in advance! David

2

There are 2 answers

5
sha On

Change your definition like this:

listeners: {
    exception: function(proxy, response, options) {                 
        this.requestMessageProcessor(proxy, response);
    },
    scope: this
},

That should do it.

2
Thiem Nguyen On

If you want to use that function as "global" one which is able to be called anytime, anywhere, then the way I usually do is to create a main controller, let's say MainController and define the function in there. Later, whenever you want to call it, just use

Ext.getController('MainController').yourFunction

If you still want to put that function into your store, simply set an id to that store and use this:

Ext.getStore('your-store-id').yourFunction

Hope it helps.