jQuery: The Write Less, Do More JavaScript Library

Append some parameters in every ajax call's query string automatically

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Is there a way to automatically append some parameters in every ajax
call's query string?

"ajaxStart" event doesn't send ajax options to callback function, so
there is not way to modify the query string.

"ajaxSend" event doesn't work with GET request, because the query
string is already appended to the url and xhr is already open, so
there is no way to change the url.

For "beforeSend", it's the same as "ajaxSend".

Set global setting in ajaxSetup will not work, if "data" is specified
in individual ajax calls.

I'd like to add parameters like 'mode=ajax' to make server respond as
little data as posible(e.g, no header, no footer), and degrade
gracefully.

I have many ajax calls to make, and adding these parameters to every
single of them manually is very tedious.

So how can I achieve this in jQuery?

Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Nathan Bubna :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


My JSON-REST plugin does this by wrapping $.ajax with other method(s).
 Just add an object of params to $.rest.data.
http://plugins.jquery.com/project/rest

You can do this by wrapping/replacing the $.ajax method yourself

(function ($) {

    var _ajax = $.ajax,
        A = $.ajax = function(options) {
            if (A.data) {
                //TODO: the appending in here, i just don't feel like
writing all of this
            }
            return _ajax(options);
        };
    };

})(jQuery);

// then make a call this:
$.ajax.data = { myParam: 'alwaysAppendThis' };
// and all your ajax calls will get that data
$.get({...})
$.post({...})
// and so on



On Tue, Sep 22, 2009 at 1:18 AM, gMinuses <gminuses@...> wrote:

>
> Is there a way to automatically append some parameters in every ajax
> call's query string?
>
> "ajaxStart" event doesn't send ajax options to callback function, so
> there is not way to modify the query string.
>
> "ajaxSend" event doesn't work with GET request, because the query
> string is already appended to the url and xhr is already open, so
> there is no way to change the url.
>
> For "beforeSend", it's the same as "ajaxSend".
>
> Set global setting in ajaxSetup will not work, if "data" is specified
> in individual ajax calls.
>
> I'd like to add parameters like 'mode=ajax' to make server respond as
> little data as posible(e.g, no header, no footer), and degrade
> gracefully.
>
> I have many ajax calls to make, and adding these parameters to every
> single of them manually is very tedious.
>
> So how can I achieve this in jQuery?
>
> Thanks.
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thank you very much! Great idea!

Here is my implementation, works so far:

(function ($) {

    var _ajax = $.ajax,
    A = $.ajax = function(options) {
        if (A.data)
                        if(options.data) {
                                if(typeof options.data !== 'string')
                                        options.data = $.param(options.data);

                                if(typeof A.data !== 'string')
                                        A.data = $.param(A.data);

                                options.data += '&' + A.data;
                        } else
                                options.data = A.data;

        return _ajax(options);
    };

})(jQuery);

On Sep 22, 10:57 pm, Nathan Bubna <nbu...@...> wrote:

> My JSON-REST plugin does this by wrapping $.ajax with other method(s).
>  Just add an object of params to $.rest.data.http://plugins.jquery.com/project/rest
>
> You can do this by wrapping/replacing the $.ajax method yourself
>
> (function ($) {
>
>     var _ajax = $.ajax,
>         A = $.ajax = function(options) {
>             if (A.data) {
>                 //TODO: the appending in here, i just don't feel like
> writing all of this
>             }
>             return _ajax(options);
>         };
>     };
>
> })(jQuery);
>
> // then make a call this:
> $.ajax.data = { myParam: 'alwaysAppendThis' };
> // and all your ajax calls will get that data
> $.get({...})
> $.post({...})
> // and so on
>
> On Tue, Sep 22, 2009 at 1:18 AM, gMinuses <gminu...@...> wrote:
>
> > Is there a way to automatically append some parameters in every ajax
> > call's query string?
>
> > "ajaxStart" event doesn't send ajax options to callback function, so
> > there is not way to modify the query string.
>
> > "ajaxSend" event doesn't work with GET request, because the query
> > string is already appended to the url and xhr is already open, so
> > there is no way to change the url.
>
> > For "beforeSend", it's the same as "ajaxSend".
>
> > Set global setting in ajaxSetup will not work, if "data" is specified
> > in individual ajax calls.
>
> > I'd like to add parameters like 'mode=ajax' to make server respond as
> > little data as posible(e.g, no header, no footer), and degrade
> > gracefully.
>
> > I have many ajax calls to make, and adding these parameters to every
> > single of them manually is very tedious.
>
> > So how can I achieve this in jQuery?
>
> > Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Shade-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I do this on two of my plugins, flXHR proxy and mpAjax. These were the
two I talked about in my jQuery Conf talk on extending Ajax, and it
comes down to exactly this method, wrapping the $.ajax call.

But, one thing I would suggest, do this instead:

(function($){
   var _ajax = $.ajax;

   $.extend({
      ajax:function(o){
         if (this.data......
         ....
         return _ajax.call(this,o);
      }
   });
})(jQuery);

I believe that's the more suggested way of extending/overriding
functions the "jQuery" way. Also, the ".call()" makes sure to preserve
the "this" binding for the original ajax call, which I think is a
safer way to deal with jQuery internals overriding.




On Sep 23, 1:52 am, gMinuses <gminu...@...> wrote:

> Thank you very much! Great idea!
>
> Here is my implementation, works so far:
>
> (function ($) {
>
>     var _ajax = $.ajax,
>     A = $.ajax = function(options) {
>         if (A.data)
>                         if(options.data) {
>                                 if(typeof options.data !== 'string')
>                                         options.data = $.param(options.data);
>
>                                 if(typeof A.data !== 'string')
>                                         A.data = $.param(A.data);
>
>                                 options.data += '&' + A.data;
>                         } else
>                                 options.data = A.data;
>
>         return _ajax(options);
>     };
>
> })(jQuery);
>
> On Sep 22, 10:57 pm, Nathan Bubna <nbu...@...> wrote:
>
>
>
> > My JSON-REST plugin does this by wrapping $.ajax with other method(s).
> >  Just add an object of params to $.rest.data.http://plugins.jquery.com/project/rest
>
> > You can do this by wrapping/replacing the $.ajax method yourself
>
> > (function ($) {
>
> >     var _ajax = $.ajax,
> >         A = $.ajax = function(options) {
> >             if (A.data) {
> >                 //TODO: the appending in here, i just don't feel like
> > writing all of this
> >             }
> >             return _ajax(options);
> >         };
> >     };
>
> > })(jQuery);
>
> > // then make a call this:
> > $.ajax.data = { myParam: 'alwaysAppendThis' };
> > // and all your ajax calls will get that data
> > $.get({...})
> > $.post({...})
> > // and so on
>
> > On Tue, Sep 22, 2009 at 1:18 AM, gMinuses <gminu...@...> wrote:
>
> > > Is there a way to automatically append some parameters in every ajax
> > > call's query string?
>
> > > "ajaxStart" event doesn't send ajax options to callback function, so
> > > there is not way to modify the query string.
>
> > > "ajaxSend" event doesn't work with GET request, because the query
> > > string is already appended to the url and xhr is already open, so
> > > there is no way to change the url.
>
> > > For "beforeSend", it's the same as "ajaxSend".
>
> > > Set global setting in ajaxSetup will not work, if "data" is specified
> > > in individual ajax calls.
>
> > > I'd like to add parameters like 'mode=ajax' to make server respond as
> > > little data as posible(e.g, no header, no footer), and degrade
> > > gracefully.
>
> > > I have many ajax calls to make, and adding these parameters to every
> > > single of them manually is very tedious.
>
> > > So how can I achieve this in jQuery?
>
> > > Thanks.- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by dave.methvin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> I'd like to add parameters like 'mode=ajax' to make server
> respond as little data as posible(e.g, no header, no footer),
> and degrade gracefully.

If you have access to the HTTP headers, just look at them. $.ajax adds
a "X-Requested-With: XMLHttpRequest" header.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Andrea Giammarchi-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Heve you tried already:

jQuery.ajaxSettings.data = {
    defaultKey:"defaultValue",
    otherKey:"otherValue"
};

???

On Tue, Sep 22, 2009 at 9:18 AM, gMinuses <gminuses@...> wrote:

Is there a way to automatically append some parameters in every ajax
call's query string?

"ajaxStart" event doesn't send ajax options to callback function, so
there is not way to modify the query string.

"ajaxSend" event doesn't work with GET request, because the query
string is already appended to the url and xhr is already open, so
there is no way to change the url.

For "beforeSend", it's the same as "ajaxSend".

Set global setting in ajaxSetup will not work, if "data" is specified
in individual ajax calls.

I'd like to add parameters like 'mode=ajax' to make server respond as
little data as posible(e.g, no header, no footer), and degrade
gracefully.

I have many ajax calls to make, and adding these parameters to every
single of them manually is very tedious.

So how can I achieve this in jQuery?

Thanks.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Andrea Giammarchi-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

or ...

jQuery.ajaxSetup({
    data:{
        defaultKey:"defaultValue",
        otherKey:"otherValue"
    }
});

...


On Wed, Sep 30, 2009 at 11:34 PM, Andrea Giammarchi <andrea.giammarchi@...> wrote:
Heve you tried already:

jQuery.ajaxSettings.data = {
    defaultKey:"defaultValue",
    otherKey:"otherValue"
};

???

On Tue, Sep 22, 2009 at 9:18 AM, gMinuses <gminuses@...> wrote:

Is there a way to automatically append some parameters in every ajax
call's query string?

"ajaxStart" event doesn't send ajax options to callback function, so
there is not way to modify the query string.

"ajaxSend" event doesn't work with GET request, because the query
string is already appended to the url and xhr is already open, so
there is no way to change the url.

For "beforeSend", it's the same as "ajaxSend".

Set global setting in ajaxSetup will not work, if "data" is specified
in individual ajax calls.

I'd like to add parameters like 'mode=ajax' to make server respond as
little data as posible(e.g, no header, no footer), and degrade
gracefully.

I have many ajax calls to make, and adding these parameters to every
single of them manually is very tedious.

So how can I achieve this in jQuery?

Thanks.




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Sorry for my late reply, this is indeed a better way. Thank you!.

On Sep 23, 9:14 pm, getify <get...@...> wrote:

> I do this on two of my plugins, flXHR proxy and mpAjax. These were the
> two I talked about in my jQuery Conf talk on extending Ajax, and it
> comes down to exactly this method, wrapping the $.ajax call.
>
> But, one thing I would suggest, do this instead:
>
> (function($){
>    var _ajax = $.ajax;
>
>    $.extend({
>       ajax:function(o){
>          if (this.data......
>          ....
>          return _ajax.call(this,o);
>       }
>    });
>
> })(jQuery);
>
> I believe that's the more suggested way of extending/overriding
> functions the "jQuery" way. Also, the ".call()" makes sure to preserve
> the "this" binding for the original ajax call, which I think is a
> safer way to deal with jQuery internals overriding.
>
> On Sep 23, 1:52 am, gMinuses <gminu...@...> wrote:
>
> > Thank you very much! Great idea!
>
> > Here is my implementation, works so far:
>
> > (function ($) {
>
> >     var _ajax = $.ajax,
> >     A = $.ajax = function(options) {
> >         if (A.data)
> >                         if(options.data) {
> >                                 if(typeof options.data !== 'string')
> >                                         options.data = $.param(options.data);
>
> >                                 if(typeof A.data !== 'string')
> >                                         A.data = $.param(A.data);
>
> >                                 options.data += '&' + A.data;
> >                         } else
> >                                 options.data = A.data;
>
> >         return _ajax(options);
> >     };
>
> > })(jQuery);
>
> > On Sep 22, 10:57 pm, Nathan Bubna <nbu...@...> wrote:
>
> > > My JSON-REST plugin does this by wrapping $.ajax with other method(s).
> > >  Just add an object of params to $.rest.data.http://plugins.jquery.com/project/rest
>
> > > You can do this by wrapping/replacing the $.ajax method yourself
>
> > > (function ($) {
>
> > >     var _ajax = $.ajax,
> > >         A = $.ajax = function(options) {
> > >             if (A.data) {
> > >                 //TODO: the appending in here, i just don't feel like
> > > writing all of this
> > >             }
> > >             return _ajax(options);
> > >         };
> > >     };
>
> > > })(jQuery);
>
> > > // then make a call this:
> > > $.ajax.data = { myParam: 'alwaysAppendThis' };
> > > // and all your ajax calls will get that data
> > > $.get({...})
> > > $.post({...})
> > > // and so on
>
> > > On Tue, Sep 22, 2009 at 1:18 AM, gMinuses <gminu...@...> wrote:
>
> > > > Is there a way to automatically append some parameters in every ajax
> > > > call's query string?
>
> > > > "ajaxStart" event doesn't send ajax options to callback function, so
> > > > there is not way to modify the query string.
>
> > > > "ajaxSend" event doesn't work with GET request, because the query
> > > > string is already appended to the url and xhr is already open, so
> > > > there is no way to change the url.
>
> > > > For "beforeSend", it's the same as "ajaxSend".
>
> > > > Set global setting in ajaxSetup will not work, if "data" is specified
> > > > in individual ajax calls.
>
> > > > I'd like to add parameters like 'mode=ajax' to make server respond as
> > > > little data as posible(e.g, no header, no footer), and degrade
> > > > gracefully.
>
> > > > I have many ajax calls to make, and adding these parameters to every
> > > > single of them manually is very tedious.
>
> > > > So how can I achieve this in jQuery?
>
> > > > Thanks.- Hide quoted text -
>
> > - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Really? I'll try that. If it's true, the whole appending-parameter
idea becomes obsolete! Thank you!

On Sep 27, 10:42 am, Dave Methvin <dave.meth...@...> wrote:
> > I'd like to add parameters like 'mode=ajax' to make server
> > respond as little data as posible(e.g, no header, no footer),
> > and degrade gracefully.
>
> If you have access to the HTTP headers, just look at them. $.ajax adds
> a "X-Requested-With: XMLHttpRequest" header.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Like I said: setting data property in ajaxSetup won't work, if it's
specified
in individual ajax calls.

But Dave Methvin said that $.ajax adds a "X-Requested-With:
XMLHttpRequest" header. I think the whole idea of appending some
parameters becomes obsolete. Anyway, thanks for the reply.

On Oct 1, 6:36 am, Andrea Giammarchi <andrea.giammar...@...>
wrote:

> or ...
>
> jQuery.ajaxSetup({
>     data:{
>         defaultKey:"defaultValue",
>         otherKey:"otherValue"
>     }
>
> });
>
> ...
>
> On Wed, Sep 30, 2009 at 11:34 PM, Andrea Giammarchi <
>
> andrea.giammar...@...> wrote:
> > Heve you tried already:
>
> > jQuery.ajaxSettings.data = {
> >     defaultKey:"defaultValue",
> >     otherKey:"otherValue"
> > };
>
> > ???
>
> > On Tue, Sep 22, 2009 at 9:18 AM, gMinuses <gminu...@...> wrote:
>
> >> Is there a way to automatically append some parameters in every ajax
> >> call's query string?
>
> >> "ajaxStart" event doesn't send ajax options to callback function, so
> >> there is not way to modify the query string.
>
> >> "ajaxSend" event doesn't work with GET request, because the query
> >> string is already appended to the url and xhr is already open, so
> >> there is no way to change the url.
>
> >> For "beforeSend", it's the same as "ajaxSend".
>
> >> Set global setting in ajaxSetup will not work, if "data" is specified
> >> in individual ajax calls.
>
> >> I'd like to add parameters like 'mode=ajax' to make server respond as
> >> little data as posible(e.g, no header, no footer), and degrade
> >> gracefully.
>
> >> I have many ajax calls to make, and adding these parameters to every
> >> single of them manually is very tedious.
>
> >> So how can I achieve this in jQuery?
>
> >> Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Andrea Giammarchi-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Tue, Oct 20, 2009 at 5:06 AM, gMinuses <gminuses@...> wrote:

Like I said: setting data property in ajaxSetup won't work, if it's
specified
in individual ajax calls.

if it's specified in individual ajax calls you don't need defaults then, otherwise append data in the url or set the data extending original one.

// custom method
jQuery.ajaxSettings.ext = (function(anonymous){
    return function ext(o){
        anonymous.prototype = this.data || {};
        var k, result = new anonymous;
        if(o){
            for(k in o)
                result[k] = o[k]
            ;
        };
        return result;
    };
})(Function());

// your defaults
jQuery.ajaxSetup({
    data:{
        defaultKey:"defaultValue",
        otherKey:"otherValue"
    }
});

console.log(jQuery.ajaxSettings.ext({test:123}));

Regards

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I'm sorry, but I don't quite understand your code.

On Oct 20, 5:16 pm, Andrea Giammarchi <andrea.giammar...@...>
wrote:

> On Tue, Oct 20, 2009 at 5:06 AM, gMinuses <gminu...@...> wrote:
>
> > Like I said: setting data property in ajaxSetup won't work, if it's
> > specified
> > in individual ajax calls.
>
> if it's specified in individual ajax calls you don't need defaults then,
> otherwise append data in the url or set the data extending original one.
>
> // custom method
> jQuery.ajaxSettings.ext = (function(anonymous){
>     return function ext(o){
>         anonymous.prototype = this.data || {};
>         var k, result = new anonymous;
>         if(o){
>             for(k in o)
>                 result[k] = o[k]
>             ;
>         };
>         return result;
>     };
>
> })(Function());
>
> // your defaults
> jQuery.ajaxSetup({
>     data:{
>         defaultKey:"defaultValue",
>         otherKey:"otherValue"
>     }
>
> });
>
> console.log(jQuery.ajaxSettings.ext({test:123}));
>
> Regards
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Scott Sauyet-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Thu, Oct 22, 2009 at 9:57 PM, gMinuses <gminuses@...> wrote:
> I'm sorry, but I don't quite understand your code.

It took me a while to get it, too.  Obviously Andrea can tell you
better, but perhaps a mere mortal can explain it better to other
mortals.  :-)

First of all, you need to know that jQuery.ajaxSetup(obj) simply
extends the jQuery.ajaxSettings object with the content of obj, so the
call to that function is defining parameters that will be supplied in
every AJAX call.  Clearly we're defining a new method named "ext" for
the jQuery.ajaxSettings object.  And in the end, we're calling that
method.  The result of that call will be an object that looks like
this:

    {
        defaultKey:"defaultValue",
        otherKey:"otherValue",
        test: 123
    }

So the only thing left is the actual definition of "ext".  Nothing to
it, right?  :-)

This is the shell:
>> jQuery.ajaxSettings.ext = (function(anonymous){
>>     // ...
>> })(Function());

This is calling Function(), which returns an anonymous function with
no body and no name (which might be considered something like a blank
constructor), and then passing that in as the parameter "anonymous" to
an unnamed function.  "ext" will be the returned value of the body of
that shell, namely

>>     return function ext(o){
>>         // ...
>>     };

So ext will be a function.

Note that the re-use of the name ext in this declaration is not
necessary.  It allows the function to refer to itself internally
without resorting to arguments.callee.  But since it's never used
here, this could just as easily have been

>>     return function(o){
>>         // ...
>>     };

The body of this function starts with

>>         anonymous.prototype = this.data || {};
>>         var k, result = new anonymous;

The "this" context is jQuery.ajaxSettings, which we've set earlier to
have a parameter "data", that contains, in this case, {defaultKey:
"defaultValue", otherKey: "otherValue"}.  When we set the prototype
for the anonymous constructor to that data object, all object created
from it will have these properties.  Now we create a new instance of
that object, called "result".

>>         if(o){
>>             for(k in o)
>>                 result[k] = o[k]
>>             ;
>>         };
>>         return result;

and populate that resulting object with any parameters we've passed
in, then return it.

Now a call to a jQuery.ajax() method can be supplied the result of
jQuery.ajaxSettings.ext() call as its "data" parameter.

Does that help?

Andrea, is that accurate?

  -- Scott

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Scott Sauyet-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


However, I'm not sure I understand the advantage of the above over the
arguably simpler:

        jQuery.ajaxSettings.ext = function(o) {
            var k, result = {};
            if(this.data) {
                for(k in this.data)
                    result[k] = this.data[k]
                ;
            }
            if(o){
                for(k in o)
                    result[k] = o[k]
                ;
            };
            return result;
        };


  -- Scott

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Andrea Giammarchi-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Scott, almost everything fine ...

On Fri, Oct 23, 2009 at 6:39 PM, Scott Sauyet <scott.sauyet@...> wrote:

So the only thing left is the actual definition of "ext".  Nothing to
it, right?  :-)

extend


Note that the re-use of the name ext in this declaration is not
necessary.

named functions are good for debugging purpose while repeated words are almost meaningless, size speaking, for gzipped code


About the other question, it's almost the same except I use a bedgeObject strategy avoiding a for in loop relying the Object.clone emulator (so 1 for in for the object rather than 2 as you did, not a big deal in any case)

Regards

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


This is very thorough! Now I understand the code, Thanks a lot!
But doe it mean I have to write code like this to make an ajax:

// Set defaults
$.ajaxSetup({
    data:{
        defaultKey:"defaultValue"
    }
});

// Make an ajax call
$.ajax({
    url: './index.html',
    data: $.ajaxSetup.ext({
        customKey: "customValue"
    }
})

This is not very neat, IMHO.

On Oct 24, 1:39 am, Scott Sauyet <scott.sau...@...> wrote:

> On Thu, Oct 22, 2009 at 9:57 PM, gMinuses <gminu...@...> wrote:
> > I'm sorry, but I don't quite understand your code.
>
> It took me a while to get it, too.  Obviously Andrea can tell you
> better, but perhaps a mere mortal can explain it better to other
> mortals.  :-)
>
> First of all, you need to know that jQuery.ajaxSetup(obj) simply
> extends the jQuery.ajaxSettings object with the content of obj, so the
> call to that function is defining parameters that will be supplied in
> every AJAX call.  Clearly we're defining a new method named "ext" for
> the jQuery.ajaxSettings object.  And in the end, we're calling that
> method.  The result of that call will be an object that looks like
> this:
>
>     {
>         defaultKey:"defaultValue",
>         otherKey:"otherValue",
>         test: 123
>     }
>
> So the only thing left is the actual definition of "ext".  Nothing to
> it, right?  :-)
>
> This is the shell:
>
> >> jQuery.ajaxSettings.ext = (function(anonymous){
> >>     // ...
> >> })(Function());
>
> This is calling Function(), which returns an anonymous function with
> no body and no name (which might be considered something like a blank
> constructor), and then passing that in as the parameter "anonymous" to
> an unnamed function.  "ext" will be the returned value of the body of
> that shell, namely
>
> >>     return function ext(o){
> >>         // ...
> >>     };
>
> So ext will be a function.
>
> Note that the re-use of the name ext in this declaration is not
> necessary.  It allows the function to refer to itself internally
> without resorting to arguments.callee.  But since it's never used
> here, this could just as easily have been
>
> >>     return function(o){
> >>         // ...
> >>     };
>
> The body of this function starts with
>
> >>         anonymous.prototype = this.data || {};
> >>         var k, result = new anonymous;
>
> The "this" context is jQuery.ajaxSettings, which we've set earlier to
> have a parameter "data", that contains, in this case, {defaultKey:
> "defaultValue", otherKey: "otherValue"}.  When we set the prototype
> for the anonymous constructor to that data object, all object created
> from it will have these properties.  Now we create a new instance of
> that object, called "result".
>
> >>         if(o){
> >>             for(k in o)
> >>                 result[k] = o[k]
> >>             ;
> >>         };
> >>         return result;
>
> and populate that resulting object with any parameters we've passed
> in, then return it.
>
> Now a call to a jQuery.ajax() method can be supplied the result of
> jQuery.ajaxSettings.ext() call as its "data" parameter.
>
> Does that help?
>
> Andrea, is that accurate?
>
>   -- Scott
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


This is a very smart strategy, never aware of that. I think I can
benefit a lot from it. Thanks for mentioning!

On Oct 24, 2:23 am, Andrea Giammarchi <andrea.giammar...@...>
wrote:

> Hi Scott, almost everything fine ...
>
> On Fri, Oct 23, 2009 at 6:39 PM, Scott Sauyet <scott.sau...@...>wrote:
>
>
>
> > So the only thing left is the actual definition of "ext".  Nothing to
> > it, right?  :-)
>
> extend
>
> Note that the re-use of the name ext in this declaration is not
>
> > necessary.
>
> named functions are good for debugging purpose while repeated words are
> almost meaningless, size speaking, for gzipped code
>
> About the other question, it's almost the same except I use a bedgeObject
> strategy avoiding a for in loop relying the Object.clone emulator (so 1 for
> in for the object rather than 2 as you did, not a big deal in any case)
>
> Regards
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Shade-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


You need to be careful of named function expressions, for IE. There's
a dangerous memory leak pattern that all versions of IE suffer from.

This code:

return function ext(o){
    // ...
};

It was explained that the use of "ext" here (which is in fact the
named function expression pattern) was to avoid the use of
arguments.callee. Unfortunately, this named function expression usage
creates a memory leak in IE.

Normally, if you were to do an expression like that, you could fix
this memory leak by assigning a null value to the named identifier.
For instance:

var foo = function ext(o) {
   // ...
};
return foo;

That code would do the same thing, and create the same memory leak.
However, it would allow you to fix it like this:

var foo = function ext(o) {
  // ...
};
ext = null; // squashes the IE memory leak with named function
expressions
return foo;

Here's the article which discusses the memory leak pattern in IE and
how to fix it:

http://yura.thinkweb2.com/named-function-expressions/

Scroll about half way down to see the discussion of JScript's (IE)
bugs with named function expressions.

Bottom line (IMHO)... even though it's technically going to be
deprecated at some point, use arguments.callee instead of named
function expressions, unless you are prepared to make sure you "fix"
this bug every time you do.





On Oct 23, 8:20 pm, gMinuses <gminu...@...> wrote:

> This is a very smart strategy, never aware of that. I think I can
> benefit a lot from it. Thanks for mentioning!
>
> On Oct 24, 2:23 am, Andrea Giammarchi <andrea.giammar...@...>
> wrote:
>
>
>
> > Hi Scott, almost everything fine ...
>
> > On Fri, Oct 23, 2009 at 6:39 PM, Scott Sauyet <scott.sau...@...>wrote:
>
> > > So the only thing left is the actual definition of "ext".  Nothing to
> > > it, right?  :-)
>
> > extend
>
> > Note that the re-use of the name ext in this declaration is not
>
> > > necessary.
>
> > named functions are good for debugging purpose while repeated words are
> > almost meaningless, size speaking, for gzipped code
>
> > About the other question, it's almost the same except I use a bedgeObject
> > strategy avoiding a for in loop relying the Object.clone emulator (so 1 for
> > in for the object rather than 2 as you did, not a big deal in any case)
>
> > Regards- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by gMinuses :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Wo{99}w! Thank you for bring it up. Never thought JScript was going to
bite me on that. Especially for "Named function expression creates TWO
DISTINCT function objects!", it's really horrifying!

On Oct 25, 5:57 am, Shade <get...@...> wrote:

> You need to be careful of named function expressions, for IE. There's
> a dangerous memory leak pattern that all versions of IE suffer from.
>
> This code:
>
> return function ext(o){
>     // ...
>
> };
>
> It was explained that the use of "ext" here (which is in fact the
> named function expression pattern) was to avoid the use of
> arguments.callee. Unfortunately, this named function expression usage
> creates a memory leak in IE.
>
> Normally, if you were to do an expression like that, you could fix
> this memory leak by assigning a null value to the named identifier.
> For instance:
>
> var foo = function ext(o) {
>    // ...};
>
> return foo;
>
> That code would do the same thing, and create the same memory leak.
> However, it would allow you to fix it like this:
>
> var foo = function ext(o) {
>   // ...};
>
> ext = null; // squashes the IE memory leak with named function
> expressions
> return foo;
>
> Here's the article which discusses the memory leak pattern in IE and
> how to fix it:
>
> http://yura.thinkweb2.com/named-function-expressions/
>
> Scroll about half way down to see the discussion of JScript's (IE)
> bugs with named function expressions.
>
> Bottom line (IMHO)... even though it's technically going to be
> deprecated at some point, use arguments.callee instead of named
> function expressions, unless you are prepared to make sure you "fix"
> this bug every time you do.
>
> On Oct 23, 8:20 pm, gMinuses <gminu...@...> wrote:
>
> > This is a very smart strategy, never aware of that. I think I can
> > benefit a lot from it. Thanks for mentioning!
>
> > On Oct 24, 2:23 am, Andrea Giammarchi <andrea.giammar...@...>
> > wrote:
>
> > > Hi Scott, almost everything fine ...
>
> > > On Fri, Oct 23, 2009 at 6:39 PM, Scott Sauyet <scott.sau...@...>wrote:
>
> > > > So the only thing left is the actual definition of "ext".  Nothing to
> > > > it, right?  :-)
>
> > > extend
>
> > > Note that the re-use of the name ext in this declaration is not
>
> > > > necessary.
>
> > > named functions are good for debugging purpose while repeated words are
> > > almost meaningless, size speaking, for gzipped code
>
> > > About the other question, it's almost the same except I use a bedgeObject
> > > strategy avoiding a for in loop relying the Object.clone emulator (so 1 for
> > > in for the object rather than 2 as you did, not a big deal in any case)
>
> > > Regards- Hide quoted text -
>
> > - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Append some parameters in every ajax call's query string automatically

by Andrea Giammarchi-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Sat, Oct 24, 2009 at 9:57 PM, Shade <getify@...> wrote:

You need to be careful of named function expressions, for IE

oh no, again ... kangaaaaaaaaaaaaaaaaaaax!!!

btw, have a look:
http://webreflection.blogspot.com/2009/10/internet-explorer-scope-resolution.html

Regards

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

< Prev | 1 - 2 | Next >