2013-08-30 98 views
0

我使用Sencha touch 2製作一個小型網絡應用程序。我已經完成了登錄頁面。第二頁的目的是獲取發佈產品的當前用戶會話。這是登錄頁面在sencha touch 2中登錄後保持登錄信息

onSignInCommand: function (view, username, password) { 
    var me = this; 
    var loginView = this.getLoginView(); 
    if (username.length == 0 || password.length == 0) { 
     loginView.showSignInMessage("Please enter your username and password."); 
     return; 
    } 

    loginView.setMasked({ 
     xtype: "loadmask", 
     message:"Signing in..." 
    }); 

    //Set ajax 
    Ext.Ajax.request({ 
     url: "./ajax/Account.ashx", 
     params: { 
      type: "login", 
      username: username, 
      password: password 
     }, 
     success: function (response) { 
      var loginResponse = Ext.JSON.decode(response.responseText); 
      if (loginResponse.success) { 
       me.sessionToken = loginResponse.sessionToken; 
       me.showSignInSuccess(); 
      } else { 
       me.sessionToken = null; 
       me.showSignInFailedMessage(loginResponse.message); 
      } 
     }, 
     failure: function() { 
      me.sessionToken = null; 
      me.showSignInFailedMessage('Login failed. Please try again later.'); 
     } 
    }); 
} 

和服務器端:

private void Login(HttpContext context) 
{ 
    var resultStt = ""; 
    var userid = context.Request["username"]; 
    var password = context.Request["password"]; 
    if(!string.IsNullOrEmpty(userid) && !string.IsNullOrEmpty(password)) 
    { 
     var user = new Select() .From<User>() 
           .Where("UserID").IsEqualTo(userid) 
           .And("UserPassword").IsEqualTo(password) 
           .ExecuteSingle<User>(); 
     if(user!=null) 
     { 
      context.Session.Add("PickerUser",user); 
      resultStt = " {\"success\":true, \"user\":{ \"userId\":"+user.UserID+", \"sessionId\":\"" + context.Session.SessionID + "\"}}"; 
     } 
     else 
     { 
      resultStt = " {\"success\":false, \"message\":\"Login failed. Please enter the correct credentials.\"}"; 
     } 
    } 
    context.Response.Write(resultStt); 
} 

第二頁,我需要獲得由用戶創建的列表procducts

store: { 
       autoload:true, 
       ... 
       proxy: { 
        type: "ajax", 
        url: "./ajax/process.ashx?type=loadassigned", 
        reader:{ 
         type:"json", 
         rootProperty: "data" 
        } 
       }     
      }, 

無法獲取對話,因爲AJAX在啓動時加載應用程序

var currenUser = context.Session["PickerUser"] as User; 
+0

只是一個建議,我建議切換到令牌的方法或類似 –

回答

0

你可以刪除配置:

autoLoad: true 

並稱之爲在你登錄成功處理函數:

Ext.getStore('yourStoresId').load({ 
    callback: function() { 
     console.log('my store has loaded'); 
    } 
});