1

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.

4

2 回答 2

1

如果.login()是通常需要凭据的第一个方法,那么您可以将其设置为该方法的必需参数,然后将凭据存储在对象中:

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"}
    );
},

然后,在其他方法中,您可以使用 访问此用户的凭据this.credentials

如果还有其他方法也可以首先调用并且需要它们的凭据,那么您可以将凭据也作为这些方法的参数,或者您可以创建一个.init()仅建立凭据的方法,或者您可以将其作为参数此对象的构造函数。


您可能还必须修复此行:

 calledUrl.post(...)

因为calledUrl是一个字符串并且字符串没有.post()方法,除非您使用某种添加一个的第 3 方库。

于 2013-10-20T22:36:08.420 回答
1

我建议您阅读 JavaScript 中的范围。如果没有更多解释你想要做什么,我会尝试这样的模式......

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();
于 2013-10-20T22:40:26.150 回答