Skip to content

Storing/retrieving the method/URL of the previous request. #2399

Closed
wants to merge 4 commits into from
View
40 Docs/Request/Request.md
@@ -248,6 +248,46 @@ Returns the given response header or null if not found.
alert(this.getHeader('Date')); // alerts the server date (for example, 'Thu, 26 Feb 2009 20:26:06 GMT')
}});
+Request Method: getUrl {#Request:getUrl}
@ibolmo
MooTools member
ibolmo added a note Aug 13, 2012

Instead of getUrl it should be getURL since it's Uniform Resource Locator

...

Then again I haven't checked other methods with acronyms for usage. @arian do you recall?

@arian
MooTools member
arian added a note Aug 13, 2012

agreed, will check if we do this consistently.

@arian
MooTools member
arian added a note Aug 13, 2012

we do with String.uniqueID, DOMEvent, Fx.CSS, .. , but not really with [...].hexToRgb. So I would say we usually do this.

@ibolmo
MooTools member
ibolmo added a note Aug 13, 2012

Thanks @arian for the research. @chrispitt just update the PR, and fix the small optimization and LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+--------------------------------------
+
+Returns the last URL requested.
+
+### Syntax:
+
+ myRequest.getUrl();
+
+### Returns:
+
+* (*string*) The value last URL requested.
+* (*null*) `null` if no request has yet been made.
+
+### Example:
+
+ var myRequest = new Request({url: 'getData.php', method: 'get', onSuccess: function(responseText, responseXML){
+ alert(this.getUrl()); // alerts 'getData.php'
+ }});
+
+Request Method: getMethod {#Request:getMethod}
+--------------------------------------
+
+Returns the method used in the last request.
+
+### Syntax:
+
+ myRequest.getMethod();
+
+### Returns:
+
+* (*string*) The method used in the last request.
+* (*null*) `null` if no request has yet been made.
+
+### Example:
+
+ var myRequest = new Request({url: 'getData.php', method: 'get', onSuccess: function(responseText, responseXML){
+ alert(this.getMethod()); // alerts 'GET'
+ }});
+
Request Method: send {#Request:send}
----------------------------
View
15 Source/Request/Request.js
@@ -134,6 +134,14 @@ var Request = this.Request = new Class({
}.bind(this));
},
+ getUrl: function(){
+ return this.url;
+ },
+
+ getMethod: function(){
+ return this.method;
+ },
+
check: function(){
if (!this.running) return true;
switch (this.options.link){
@@ -166,6 +174,11 @@ var Request = this.Request = new Class({
data = (data) ? format + '&' + data : format;
}
+ var upperMethod = method.toUpperCase();
+
+ this.method = upperMethod;
+ this.url = url;
+
if (this.options.emulation && !['get', 'post'].contains(method)){
var _method = '_method=' + method;
data = (data) ? _method + '&' + data : _method;
@@ -196,7 +209,7 @@ var Request = this.Request = new Class({
xhr.onprogress = this.progress.bind(this);
}
- xhr.open(method.toUpperCase(), url, this.options.async, this.options.user, this.options.password);
+ xhr.open(upperMethod, url, this.options.async, this.options.user, this.options.password);
@ibolmo
MooTools member
ibolmo added a note Aug 13, 2012
this.method = method.toUpperCase();
this.url = url;

// ...

xhr.open(this.method, url, // ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
if (this.options.user && 'withCredentials' in xhr) xhr.withCredentials = true;
xhr.onreadystatechange = this.onStateChange.bind(this);
View
35 Specs/1.3client/Request/Request.js
@@ -136,5 +136,40 @@ describe('Request', function(){
});
+ it('should store last method and url of request on send', function(){
+
+ var firstUrl = '../Helpers/request.php?first',
+ firstMethod = 'GET',
+
+ secondUrl = '../Helpers/request.php?second',
+ secondMethod = 'POST',
+
+ request = new Request({
+ url: firstUrl,
+ method: firstMethod,
+ data: {
+ '__response': 'hello world'
+ }
+ }),
+
+ fake = this.requests[0];
+
+ request.send();
+ fake.respond(200, {'Content-Type': 'text/plain'});
+
+ expect(request.url).toBe(firstUrl);
+ expect(request.method).toBe(firstMethod);
+
+ request.setOptions({
+ url: secondUrl,
+ method: secondMethod
+ }).send();
+ fake.respond(200, {'Content-Type': 'text/plain'});
+
+ expect(request.getUrl()).toBe(secondUrl);
+ expect(request.getMethod()).toBe(secondMethod);
+
+ });
+
});
Something went wrong with that request. Please try again.