Reputation: 5855
I have some simple javascript functions to interact with an API like this one to login:
login: function(username, password) {
var calledUrl = baseapi + "user/login/" + credentials;
calledUrl.post(
function (content) {
/*console.log("success" + JSON.stringify(content, null, 4));*/
},
function (e) {
console.log("it failed! -> " + e);
},
{
"username": username,
"password": password
},
{"Accept" : "application/json"}
);
},
The problem is, in the URL I must pass some credentials and they look like that:
var credentials = "?api_username=" + api_username + "&api_key=" + api_key;
Right now this variable is hardcoded to make some tests but of course it should change for each person using the function. I don't want to ask for it with each request, in this case I only want to ask for username
and password
. I would like to ask for it once and for all during an initializing process or whatever it is called and then remember it when executing the various functions.
Upvotes: 1
Views: 339
Reputation: 707238
If .login()
is the first method that typically needs the credentials, then you can just make it a required argument for that method and then store the credentials in the object:
login: function(username, password, credentials) {
// save credentials for use in other methods
this.credentials = credentials;
var calledUrl = baseapi + "user/login/" + credentials;
calledUrl.post(
function (content) {
/*console.log("success" + JSON.stringify(content, null, 4));*/
},
function (e) {
console.log("it failed! -> " + e);
},
{
"username": username,
"password": password
},
{"Accept" : "application/json"}
);
},
Then, in other methods, you can access the credentials for this user with this.credentials
.
If there are other methods that could also be called first and need the credentials for them, then you can either make the credentials an argument for those also or you can create a .init()
method that just establishes the credentials or you can make it an argument in the constructor for this object.
You will probably also have to fix this line:
calledUrl.post(...)
because calledUrl
is a string and strings don't have a .post()
method unless you're using some sort of 3rd party library that adds one.
Upvotes: 1
Reputation: 154
I recommend that you read about scope in JavaScript. Without more explanation of what you are trying to do, I would try something like this pattern...
var app = {
baseapi: 'http://some.url.com'
/* assuming the api user/pass are different form the account trying to log in */
,api_username: ''
,api_key: ''
,username: ''
,userpass: ''
,get_creditialString: function() {
return '?api_username=' + this.api_username + '&api_key=' + this.api_key;
}
,init: function(){
// do something to prompt for username and password
this.username = 'myUserName';
this.userpass = 'supersecretpassword';
this.login();
}
,login: function() {
var calledUrl = this.baseapi + "user/login/" + this.get_credentialString();
calledUrl.post(
function (content) {
/*console.log("success" + JSON.stringify(content, null, 4));*/
},
function (e) {
console.log("it failed! -> " + e);
},
{
"username": this.username,
"password": this.userpass
},
{"Accept" : "application/json"}
);
}
}
app.init();
Upvotes: 1