Skip to content

Commit

Permalink
Make bind work with only jQuery without prototype if needed
Browse files Browse the repository at this point in the history
If jQuery is included before as an adjunct and jQuery is the
 implementation behind $; the jQuery.ajax call will be used.
 If neither is included earlier then prototype will be used.

 jQuery doesn't have a "to JSON string" function so JSON.stringify
 is used and to secure older browsers the json2 script is included.
  • Loading branch information
rsandell committed Mar 28, 2013
1 parent 117ae8e commit 48cdf0f
Show file tree
Hide file tree
Showing 8 changed files with 607 additions and 16 deletions.
47 changes: 33 additions & 14 deletions core/src/main/resources/org/kohsuke/stapler/bind.js
@@ -1,5 +1,4 @@
// included into the page as an adjunct
// @include org.kohsuke.stapler.framework.prototype.prototype
// bind tag takes care of the dependency as an adjunct

function makeStaplerProxy(url,crumb,methods) {
if (url.substring(url.length - 1) !== '/') url+='/';
Expand All @@ -21,19 +20,39 @@ function makeStaplerProxy(url,crumb,methods) {
for (var i=0; i<args.length-(callback!=null?1:0); i++)
a.push(args[i]);

new Ajax.Request(url+methodName, {
method: 'post',
requestHeaders: {'Content-type':'application/x-stapler-method-invocation;charset=UTF-8','Crumb':crumb},
postBody: Object.toJSON(a),
onSuccess: function(t) {
if (callback!=null) {
t.responseObject = function() {
return eval('('+this.responseText+')');
};
callback(t);
if(window.jQuery === window.$) { //Is jQuery the active framework?
$.ajax({
type: "POST",
url: url+methodName,
data: JSON.stringify(a),
contentType: 'application/x-stapler-method-invocation;charset=UTF-8',
headers: {'Crumb':crumb},
dataType: "json",
success: function(data, textStatus, jqXHR) {
if (callback!=null) {
var t = {};
t.responseObject = function() {
return data;
};
callback(t);
}
}
}
});
});
} else { //Assume prototype should work
new Ajax.Request(url+methodName, {
method: 'post',
requestHeaders: {'Content-type':'application/x-stapler-method-invocation;charset=UTF-8','Crumb':crumb},
postBody: Object.toJSON(a),
onSuccess: function(t) {
if (callback!=null) {
t.responseObject = function() {
return eval('('+this.responseText+')');
};
callback(t);
}
}
});
}
}
};

Expand Down

3 comments on commit 48cdf0f

@jglick
Copy link
Member

@jglick jglick commented on 48cdf0f Jul 16, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to have introduced: JENKINS-18641

@jan-molak
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changeset will break any plugin relying on makeStaplerProxy to work as advertised.

@rsandell
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn. That was one of the cases that I couldn't test when I did the implementation.

Please sign in to comment.