Skip to content

Instantly share code, notes, and snippets.

@ayoung
Created December 20, 2011 20:08
Show Gist options
  • Save ayoung/1503056 to your computer and use it in GitHub Desktop.
Save ayoung/1503056 to your computer and use it in GitHub Desktop.
closure problem
var rest = require('restler');
function Client(baseUrl) {
if (this instanceof Client) {
this.baseUrl = baseUrl;
}
else {
return new Client(baseUrl);
}
};
/*
* Performs an HTTP get to the given path.
* @param {string} path: the url to get
* @param {object} data: the data to send
* @param {function} callback: the callback to run on complete.
* if null it will run this.callback
*/
Client.prototype.get = function(path) {
var self = this;
return function() {
rest.get(self.baseUrl + path).on('complete', this.callback);
}
};
/*
* Performs an HTTP post to the given path.
* @param {string} path: the url to post to
* @param {object} data: the data to send
* @param {function} callback: the callback to run on complete.
* if null it will run this.callback
*/
Client.prototype.post = function(path, data, callback) {
var self = this;
var cb = callback;
return function() {
console.log(path);
console.log(cb);
rest.post(self.baseUrl + path, data).on('complete', cb || this.callback);
}
};
/*
* Performs an HTTP post to the given path.
* @param {string} path: the url to post to
* @param {object} data: the data to send
* @param {function} callback: the callback to run on complete.
* if null it will run this.callback
*/
Client.prototype.put = function(path, data) {
var self = this;
return function() {
rest.put(self.baseUrl + path, data).on('complete', this.callback);
}
};
module.exports = Client;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment