Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#11852 closed feature (notabug)

Use context parameter in .load

Reported by: anonymous Owned by: jaubourg
Priority: low Milestone: None
Component: ajax Version: 1.7.2
Keywords: Cc: jaubourg
Blocked by: Blocking:

Description

I know this is very trivial, but it is an enhancement never the less.

The .load function (ajax) assigns this to self on line 7159.

It would be better to use the context parameter of the $.ajax function as follows and then omitting the assignment to self.

jQuery.ajax({
        url: url,
        type: type,
        dataType: "html",
        data: params,
        context: this,
        // Complete callback (responseText is used internally)
        complete: function( jqXHR, status, responseText ) {
                // Store the response as specified by the jqXHR object
                responseText = jqXHR.responseText;
                // If successful, inject the HTML into all the matched elements
                if ( jqXHR.isResolved() ) {
                        // #4825: Get the actual response in case
                        // a dataFilter is present in ajaxSettings
                        jqXHR.done(function( r ) {
                                responseText = r;
                        });
                        // See if a selector was specified
                        // (Target element is this - coming from context)
                        this.html( selector ?
                                // Create a dummy div to hold the results
                                jQuery("<div>")
                                        // inject the contents of the document in, removing the scripts
                                        // to avoid any 'Permission Denied' errors in IE
                                        .append(responseText.replace(rscript, ""))

                                        // Locate the specified elements
                                        .find(selector) :

                                // If not, just inject the full result
                                responseText );
                }

                if ( callback ) {
                        this.each( callback, [ responseText, status, jqXHR ] );
                }
        }
});

Patch: http://pastebin.com/A5cYSQKV

In case you are interested where this could have any relevance see this: http://stackoverflow.com/a/10864327/1238764

Change History (8)

comment:1 Changed 7 years ago by dmethvin

Cc: jaubourg added
Component: unfiledajax
Milestone: None1.8
Priority: undecidedlow
Status: newopen

Seems like a good idea to me ... jaubourg?

comment:2 Changed 7 years ago by dmethvin

Owner: set to jaubourg
Status: openassigned

comment:3 Changed 7 years ago by dmethvin

Milestone: 1.81.8.1

comment:4 Changed 7 years ago by dmethvin

Milestone: 1.8.11.9

comment:5 Changed 7 years ago by dmethvin

Type: enhancementfeature

Bulk change from enhancement to feature.

comment:6 Changed 7 years ago by dmethvin

Resolution: notabug
Status: assignedclosed

Since this would change the callbackContext of the global events fired by $.ajax I think it's better not to land it just in case some code would be confused by the change.

https://github.com/jquery/jquery/blob/67df705bf5205fa075ba6ceee2d7fd4763894a16/src/ajax.js#L358

comment:7 Changed 7 years ago by jaubourg

Damn, it's been 6 months and I didn't answer to this one? Oo

Totally agree with dmethvin on not breaking backward compatibility here.

comment:8 Changed 7 years ago by dmethvin

Milestone: 1.9None
Note: See TracTickets for help on using tickets.