public
Created

closure problem

  • Download Gist
client.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.