0

我正在尝试使用合金中的模型进行身份验证。从昨天开始,我一直在尝试解决这个问题。如果有人可以帮助我,我将不胜感激。所以,我有一个视图login.xml,然后是一个控制器login.jslogin.js包含以下功能:

var user = Alloy.Models.user; //my user.js model
function login(e) {
    if($.username.value !== '' && $.password.value !== ''){
        if(user.login($.username.value, $.password.value)){
            Alloy.createController('home').getView().open();
            $.login.close();
        }
    }else{
        alert('Username and/or Password required!');
    }
}

然后在我的user.js模型中,它是这样的:

extendModel : function(Model) {
    _.extend(Model.prototype, {
        login: function(username, password) {
            var first_name, last_name, email;
            var _this = this;
            var url = 'http://myurl.com/test.php';
            var auth = Ti.Network.createHTTPClient({
                onerror: function(e){
                    alert(e.error);
                },
                onload: function(){
                    var json = this.responseText;
                    var response = JSON.parse(json);
                    if(response.logged == true){
                        first_name = response.f_name;
                        last_name = response.l_name;
                        email = response.email;
                        _this.set({
                            loggedIn: 1,
                            username: email,
                            realname: first_name + ' ' + last_name,
                            email: email,
                        });
                        _this.save();
                    }else{
                        alert(response.message);
                    }
                },
            });

            auth.open('POST', url);
            var params = {
                usernames: username,
                passwords: password,
            };
            auth.send(params);
            alert(_this.get('email')); //alert email
        },
    });

当我点击登录时,它login.xml会调用login. index.js所以,现在我的问题是,当我第一次单击该按钮时,我收到一个空警报alert(_this.get('email')),但是当我第二次单击该按钮时,一切正常,它会提醒电子邮件。我不知道是怎么回事。感谢您的帮助。

4

1 回答 1

1

我想我想通了,对于那些可能偶然发现同样问题的人。我使用回调函数来做到这一点。

请参阅此Titanium HTTP 请求

现在我的user.js样子是这样的:

extendModel : function(Model) {
_.extend(Model.prototype, {
    login: function(username, password, callback) {
        var first_name, last_name, email;
        var _this = this;
        var url = 'http://myurl.com/test.php';
        var auth = Ti.Network.createHTTPClient({
            onerror: function(e){
                alert(e.error);
            },
            onload: function(){
                var json = this.responseText;
                var response = JSON.parse(json);
                if(response.logged == true){
                    first_name = response.f_name;
                    last_name = response.l_name;
                    email = response.email;
                    _this.set({
                        loggedIn: 1,
                        username: email,
                        realname: first_name + ' ' + last_name,
                        email: email,
                    });
                    _this.save();
                    callback(foo); //whatever you want to send

                }else{
                    alert(response.message);
                }
            },
        });

        auth.open('POST', url);
        var params = {
            usernames: username,
            passwords: password,
        };
        auth.send(params);
    },
});

我的login.js样子是这样的:

var user = Alloy.Models.user; //my user.js model
function login(e) {
    if($.username.value !== '' && $.password.value !== ''){
        var logged_in = user.login($.username.value, $.password.value, function(foo){
            if(foo == bar)
                call_another_function(); 
        });
    }else{
       alert('Username and/or Password required!');
   }
}

谢谢。我希望这有帮助。

于 2013-10-31T16:39:15.447 回答