2012-06-08 59 views
0

我有一個愚蠢的問題,我希望你給我一隻手。提前感謝。使用sencha touch 2.0登錄後載入另一個視圖

情況如下:我有2個wiews(均使用sencha architect 2.0創建),一個用於登錄,另一個用於一般目的。我想在嘗試登錄時加載成功響應的第二個視圖,即在任何成功登錄之後。主要問題是我已經使用Ex.create,Ext.Viewport.add,Ext.Viewport.setActiveItem嘗試過,但我無法設法讓第二個視圖出現在屏幕上,登錄屏幕保持在那裏,應用程序不會加載其他視圖。另一件事,我不必爲此使用導航視圖。

這是我的控制器的代碼,我想從中加載我的第二個視圖。正如你所看到的,我甚至創建的視圖的參考,這AUTOCREATE啓用,並具有ID「mainTabPanel」:

Ext.define('MyApp.controller.Login', { 
extend: 'Ext.app.Controller', 
config: { 
    refs: { 
     loginButton: { 
      selector: '#login', 
      xtype: 'button' 
     }, 
     username: { 
      selector: '#user', 
      xtype: 'textfield' 
     }, 
     password: { 
      selector: '#pass', 
      xtype: 'passwordfield' 
     }, 
     mainTabPanel: { 
      selector: '#mainTabPanel', 
      xtype: 'tabpanel', 
      autoCreate: true 
     } 
    }, 

    control: { 
     "loginButton": { 
      tap: 'onLoginButtonTap' 
     } 
    } 
}, 

onLoginButtonTap: function(button, e, options) { 
    Ext.Ajax.request({ 
     url: '../../backend/auth.php', 

     method: 'POST', 

     params: { 
      user: this.getUsername().getValue(), 
      pass: this.getPassword().getValue() 
     }, 

     success: function(response) { 
      var json = Ext.decode(response.responseText); 
      if (json.type == 'success') { 
       // LOAD THE DAMN SECOND VIEW HERE! 
       //var paneltab = Ext.create('MyApp.view.MainTabPanel'); 
       //Ext.Viewport.add(paneltab); 
       //Ext.Viewport.setActiveItem(this.getMainTabPanel()); 
      } else { 
       alert(json.value); 
      } 
     }, 

     failure: function(response) { 
      alert('The request failed!'); 
     } 
    }); 
} 
}); 

這裏是我的登錄視圖的代碼:

Ext.define('MyApp.view.LoginForm', { 
extend: 'Ext.form.Panel', 

config: { 
    id: 'loginForm', 
    ui: 'light', 
    items: [ 
     { 
      xtype: 'fieldset', 
      ui: 'light', 
      title: 'Log into the system', 
      items: [ 
       { 
        xtype: 'textfield', 
        id: 'user', 
        label: 'User', 
        name: 'user' 
       }, 
       { 
        xtype: 'passwordfield', 
        id: 'pass', 
        label: 'Pass', 
        name: 'pass' 
       } 
      ] 
     }, 
     { 
      xtype: 'button', 
      id: 'login', 
      ui: 'confirm', 
      text: 'Login' 
     } 
    ] 
} 
}); 

最後,我想加載的視圖的代碼。這種觀點加載正常,如果我將其設置爲初始視圖,但不加載時成功登錄時:

Ext.define('MyApp.view.MainTabPanel', { 
extend: 'Ext.tab.Panel', 

config: { 
    id: 'mainTabPanel', 
    layout: { 
     animation: 'slide', 
     type: 'card' 
    }, 
    items: [ 
     { 
      xtype: 'container', 
      layout: { 
       type: 'vbox' 
      }, 
      title: 'Tab 1', 
      iconCls: 'time', 
      items: [ 
       { 
        xtype: 'titlebar', 
        docked: 'top', 
        title: 'General Report', 
        items: [ 
         { 
          xtype: 'button', 
          iconCls: 'refresh', 
          iconMask: true, 
          text: '', 
          align: 'right' 
         } 
        ] 
       }, 
       { 
        xtype: 'container', 
        height: 138, 
        flex: 1, 
        items: [ 
         { 
          xtype: 'datepickerfield', 
          label: 'From', 
          placeHolder: 'mm/dd/yyyy' 
         }, 
         { 
          xtype: 'datepickerfield', 
          label: 'To', 
          placeHolder: 'mm/dd/yyyy' 
         }, 
         { 
          xtype: 'numberfield', 
          label: 'Hours' 
         } 
        ] 
       }, 
       { 
        xtype: 'dataview', 
        ui: 'dark', 
        itemTpl: [ 
         '<div style="height:50px; background-color: white; margin-bottom: 1px;">', 
         ' <span style="color: #0000FF">{user}</span>', 
         ' <span>{description}</span>', 
         '</div>' 
        ], 
        store: 'hoursStore', 
        flex: 1 
       } 
      ] 
     }, 
     { 
      xtype: 'container', 
      title: 'Tab 2', 
      iconCls: 'maps' 
     }, 
     { 
      xtype: 'container', 
      title: 'Tab 3', 
      iconCls: 'favorites' 
     } 
    ], 
    tabBar: { 
     docked: 'bottom' 
    } 
} 
}); 

請,我需要幫助... :) 我卡在這裏像3天現在無法弄清楚問題所在。謝謝。

回答

0

銷燬登錄面板,你並不真的需要它了......

success: function(response) { 
     var json = Ext.decode(response.responseText); 

     if (json.type == 'success') { 
      // LOAD THE DAMN SECOND VIEW HERE! 
      var paneltab = Ext.create('MyApp.view.MainTabPanel'); 
      Ext.getCmp('loginForm').destroy(); 
      Ext.Viewport.add(paneltab); 

     } else { 
      alert(json.value); 
     } 
    }, 
2

難道你也發表您的app.js? 我在這裏嘗試你的代碼,並按預期加載視圖。這裏是我的app.js:

Ext.application({ 
    name: 'MyApp', 

    requires: [ 
     'Ext.MessageBox' 
    ], 
    controllers: ['Login'], 
    views: ['LoginForm','MainTabPanel'], 

    icon: { 
     '57': 'resources/icons/Icon.png', 
     '72': 'resources/icons/Icon~ipad.png', 
     '114': 'resources/icons/[email protected]', 
     '144': 'resources/icons/[email protected]' 
    }, 

    isIconPrecomposed: true, 

    startupImage: { 
     '320x460': 'resources/startup/320x460.jpg', 
     '640x920': 'resources/startup/640x920.png', 
     '768x1004': 'resources/startup/768x1004.png', 
     '748x1024': 'resources/startup/748x1024.png', 
     '1536x2008': 'resources/startup/1536x2008.png', 
     '1496x2048': 'resources/startup/1496x2048.png' 
    }, 

    launch: function() { 
     // Destroy the #appLoadingIndicator element 
     Ext.fly('appLoadingIndicator').destroy(); 

     // Initialize the main view 
     //Ext.Viewport.add(Ext.create('MyApp.view.Main')); 
     Ext.Viewport.add(Ext.create('MyApp.view.LoginForm')); 
    }, 

    onUpdated: function() { 
     Ext.Msg.confirm(
      "Application Update", 
      "This application has just successfully been updated to the latest version. Reload now?", 
      function(buttonId) { 
       if (buttonId === 'yes') { 
        window.location.reload(); 
       } 
      } 
     ); 
    } 
}); 
0

有幾個問題在這裏:

  • 你自動創建規則使用的的xtype:這將永遠只能創建一個香草Ext.TabPanel沒有項目「一個tabpanel」在裏面。您需要爲您的MyApp.view.MainTabPanel指定xtype屬性,如xtype:'mainTabPanel',然後在您的autoCreate規則中使用該xtype值。

  • 這就解釋了爲什麼這段代碼不起作用,因爲this.getMainTabPanel()會返回錯誤的對象。更簡單的是隻使用Ext.Viewport.setActiveItem(paneltab)。

    • 通常,您通常不希望爲視圖分配一個id(id:'mainTabPanel')。更安全地使用xtype來獲取它。雖然在這種情況下你不需要它,但是避免使用全局id可以創建一個視圖的多個實例(或任何其他的類類型)。