我对使用 Sencha Touch 2 库进行开发完全陌生。我按照本教程帮助创建了一个简单的登录脚本(http://miamicoder.com/2012/adding-a-login-screen-to-a-sencha-touch-application-part-2/)。我不确定的一件事是如何检查会话令牌是否存在于客户端,以便当他们返回应用程序时,登录屏幕不会显示他们是否已通过身份验证。
应用程序.js
Ext.application({
name: 'RecruitTalkTouch',
views: ['Login', 'MainMenu'],
controllers: ['Login'],
launch: function () {
Ext.Viewport.add([
{ xtype: 'loginview' },
{ xtype: 'mainmenuview' }
]);
}
});
Login.js 控制器:
Ext.define('RecruitTalkTouch.controller.Login', {
extend: 'Ext.app.Controller',
config: {
refs: {
loginView: 'loginview',
mainMenuView: 'mainmenuview'
},
control: {
loginView: {
signInCommand: 'onSignInCommand'
},
mainMenuView: {
signOffCommand: 'onSignOffCommand'
}
}
},
onSignInCommand: function(view, username, password) {
var me = this,
loginView = me.getLoginView();
if(username.length === 0 || password.length === 0) {
loginView.showSignInFailedMessage('Please enter your username and password.');
return;
}
loginView.setMasked({
xtype: 'loadmask',
message: 'Signing In...'
});
Ext.Ajax.request({
url: 'http://localhost:3000/api/v1/sessions.json',
method: 'POST',
useDefaultXhrHeader: false,
params: {
login: username,
password: password
},
success: function(resp) {
var json = Ext.JSON.decode(resp.responseText);
if(json.success === "true") {
me.sessionToken = json.auth_token;
me.signInSuccess();
} else {
me.signInFailure(json.message)
}
},
failure: function(resp) {
me.sessionToken = null;
me.signInFailure('Login failed. Please try again');
}
});
},
signInSuccess: function() {
console.log("Signed In");
var loginView = this.getLoginView(),
mainMenuView = this.getMainMenuView();
loginView.setMasked(false);
Ext.Viewport.animateActiveItem(mainMenuView, this.transition('slide', 'left'));
},
signInFailure: function(message) {
var loginView = this.getLoginView();
loginView.showSignInFailedMessage(message);
loginView.setMasked(false);
},
transition: function(type, direction) {
return { type: type, direction: direction };
},
onSignOffCommand: function() {
var me = this;
me.sessionToken = null;
Ext.Viewport.animateActiveItem(this.getLoginView(), this.transition('slide', 'right'));
}
});
Login.js 查看:
Ext.define('RecruitTalkTouch.view.Login', {
extend: 'Ext.form.Panel',
alias: "widget.loginview",
requires: ['Ext.form.FieldSet', 'Ext.form.Password', 'Ext.Label', 'Ext.Button'],
config: {
title: 'Login',
items: [
{
xtype: 'label',
html: 'Login failed. Please enter the correct credentials.',
itemId: 'signInFailed',
hidden: true,
hideAnimation: 'fadeOut',
showAnimation: 'fadeIn'
},
{
xtype: 'fieldset',
title: 'Login',
items: [
{
xtype: 'textfield',
placeHolder: 'Username',
itemId: 'userNameTextField',
name: 'userNameTextField',
required: true
},
{
xtype: 'passwordfield',
placeHolder: 'Password',
itemId: 'passwordTextField',
name: 'passwordTextField',
required: true
}
]
},
{
xtype: 'button',
itemId: 'logInButton',
ui: 'action',
padding: '10px',
text: 'Log In'
}
],
listeners: [{
delegate: '#logInButton',
event: 'tap',
fn: 'onLogInButtonTap'
}]
},
onLogInButtonTap: function() {
var me = this,
usernameField = me.down('#userNameTextField'),
passwordField = me.down('#passwordTextField'),
label = me.down('#signInFailed'),
username = usernameField.getValue(),
password = passwordField.getValue();
label.hide();
var task = Ext.create('Ext.util.DelayedTask', function(){
label.setHtml('');
me.fireEvent('signInCommand', me, username, password);
usernameField.setValue('');
passwordField.setValue('');
});
task.delay(500);
},
showSignInFailedMessage: function(message) {
var label = this.down('#signInFailed');
label.setHtml(message);
label.show();
}
});