PDA

View Full Version : data.connection custom request object



clifficious
3 May 2013, 7:49 AM
Hi,

I have several Ajax.requests in my application. Some options are the same and some are dynamically added. Now I was trying to extend a class, so that the fixed options are always set. But it gives me errors when applying the following simplified code.


Ext.define('My.own.Options', {
extend: 'Object',

headers: {
'Authorization' : 'some Text',
'Accept-Language' : 'some more Text'
},

});

When I then use this class like

Ext.Ajax.request(Ext.create('My.own.Options',{
method: 'GET',
url: someURL,
success: function(response) {
do something here...
}
},
failure: function() {
do something else here
}
}));

The error is Uncaught TypeError: Cannot call method 'indexOf' of null

Am I missing something? Thanks for any hints!

chamacs
3 May 2013, 10:55 AM
Ext.define('My.own.Options', {
extend: 'Object',

headers: {
'Authorization' : 'some Text',
'Accept-Language' : 'some more Text'
},

});


I believe that the above class must, at a minimum, extend Ext.Base.

clifficious
5 May 2013, 11:32 PM
First of all, thanks for your answer. Unfortunately the error still exists. Changing 'Object' to 'Ext.Base' did not solve it.

evant
5 May 2013, 11:50 PM
Is there any reason you're declaring a class? Seems like overkill.

clifficious
6 May 2013, 12:10 AM
Is there any reason you're declaring a class? Seems like overkill.

Well, I simplified the example. I have a lot of request which contain an authorization and some other stuff in the header. Now every time I make a request I have to manually add these data to the request config. In case anything changes in the request, I have to change it in all spots. So I want to have one central place, where it is configured. I think this goes along with common programming paradigms.

Also other programmers on the project need to use the request. Therefore I want to make it as easy as possible to use, so the others do not have to think about the fixed header configuration.

Anyway. Creating an options object and passing it to the request should work.

evant
6 May 2013, 12:13 AM
It should, but why not:



Myapp.ajaxRequest = function(options) {
Ext.apply(options, {
// defaults
});
};


Anyway, where is the exception being thrown? Without context it's not really helpful.

clifficious
6 May 2013, 12:31 AM
It should, but why not:



Myapp.ajaxRequest = function(options) {
Ext.apply(options, {
// defaults
});
};


Anyway, where is the exception being thrown? Without context it's not really helpful.

Thanks for your answer! But I do not get your code snippet. Where would I use it?

The error occurs when I try to call


Ext.Ajax.request(Ext.create('My.own.Options',{...}));

mentioned above.

evant
6 May 2013, 5:24 AM
Yes, but where in the code? indexOf gets used hundreds of times.

clifficious
6 May 2013, 11:10 PM
Yes, but where in the code? indexOf gets used hundreds of times.

Sorry, my fault. I wasn't sure what you ment with context. The error is in 'ext-all-debug.js:988'

But after some debugging I found out, that Ext.create(..) in my case does not create a proper object. I compared it with 'new Object()' and it is not the same, nor anyway similar. Am I missing a call to some parent class?

Ext.String.urlAppendext-all-debug.js:988 (http://extjs.cachefly.net/ext-4.2.0-gpl/ext-all-debug.js)
Ext.apply.urlAppendext-all-debug.js:7195 (http://extjs.cachefly.net/ext-4.2.0-gpl/ext-all-debug.js)
Ext.define.setOptionsext-all-debug.js:32327 (http://extjs.cachefly.net/ext-4.2.0-gpl/ext-all-debug.js)
Ext.define.requestext-all-debug.js:32027 (http://extjs.cachefly.net/ext-4.2.0-gpl/ext-all-debug.js)

evant
6 May 2013, 11:16 PM
You didn't specify a version, so a line number isn't helpful.

Anyway, the likelihood is that you're not passing a url in. By default, a class won't do anything in the constructor, so anything you're passing in is just ignored.



Ext.define('Overkill', {
someDefault: 'foo',

constructor: function(cfg){
Ext.apply(this, cfg);
this.callParent();
}
})

Ext.onReady(function() {

Ext.Ajax.request(new Overkill({
url: 'bar'
}))

});


However as I said above, creating a class instance is overkill, you should just have some kind of wrapper around Ajax that applies your defaults.

clifficious
6 May 2013, 11:29 PM
Thanks a lot evant. That made the job!

However, as you pointed out that it is a kind of overkill, can you give me some hints for a suitable wrapper? I would need to wrap the options object, that is passed to Ext.Ajax.request(), right? I was trying to do that with this class because I do not have a clue how to solve it in another way.

Thanks once again!

evant
6 May 2013, 11:33 PM
Something like this:



Ext.define('MyApp.data.Ajax', {
singleton: true,

defaultOptions: {
},

request: function(options) {
options = Ext.apply({}, options);
Ext.applyIf(options, this.defaultOptions);
Ext.Ajax.request(options);
}
});

clifficious
7 May 2013, 12:00 AM
Something like this:



Ext.define('MyApp.data.Ajax', {
singleton: true,

defaultOptions: {
},

request: function(options) {
options = Ext.apply({}, options);
Ext.applyIf(options, this.defaultOptions);
Ext.Ajax.request(options);
}
});


oh boy. You're right. That looks a lot more simple than my approach. Will try that later on! Thanks!