1. #1
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    122
    Vote Rating
    3
    Answers
    3
    clifficious is on a distinguished road

      0  

    Default Answered: data.connection custom request object

    Answered: data.connection custom request object


    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.

    Code:
    Ext.define('My.own.Options', {    
        extend: 'Object',
        
        headers: {
            'Authorization' : 'some Text',
            'Accept-Language' : 'some more Text'
        },
        
    });
    When I then use this class like
    Code:
    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!

  2. 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.

    Code:
    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.

  3. #2
    Touch Premium Member
    Join Date
    Mar 2011
    Posts
    123
    Vote Rating
    7
    Answers
    15
    chamacs is on a distinguished road

      0  

    Default


    Quote Originally Posted by clifficious View Post
    Code:
    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.

  4. #3
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    122
    Vote Rating
    3
    Answers
    3
    clifficious is on a distinguished road

      0  

    Default


    First of all, thanks for your answer. Unfortunately the error still exists. Changing 'Object' to 'Ext.Base' did not solve it.

  5. #4
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,064
    Vote Rating
    659
    Answers
    471
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Is there any reason you're declaring a class? Seems like overkill.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  6. #5
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    122
    Vote Rating
    3
    Answers
    3
    clifficious is on a distinguished road

      0  

    Default


    Quote Originally Posted by evant View Post
    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.

  7. #6
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,064
    Vote Rating
    659
    Answers
    471
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    It should, but why not:

    Code:
    Myapp.ajaxRequest = function(options) {
        Ext.apply(options, {
            // defaults
        });
    };
    Anyway, where is the exception being thrown? Without context it's not really helpful.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  8. #7
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    122
    Vote Rating
    3
    Answers
    3
    clifficious is on a distinguished road

      0  

    Default


    Quote Originally Posted by evant View Post
    It should, but why not:

    Code:
    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

    Code:
    Ext.Ajax.request(Ext.create('My.own.Options',{...}));
    mentioned above.

  9. #8
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,064
    Vote Rating
    659
    Answers
    471
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Yes, but where in the code? indexOf gets used hundreds of times.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  10. #9
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    122
    Vote Rating
    3
    Answers
    3
    clifficious is on a distinguished road

      0  

    Default


    Quote Originally Posted by evant View Post
    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?

  11. #10
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,064
    Vote Rating
    659
    Answers
    471
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    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.

    Code:
    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.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!