用extjs6.0寫一個系統登入及登出

來源:互聯網
上載者:User

標籤:todo   瀏覽器   app.js   form   目錄   bind   狀態   control   pad   

 

本文基於ext-6.0.0

一、用sencha cmd建立一個ExtJs小項目

  首先,需要一個命令列工具。進入extjs所在目錄。

  然後,輸入:sencha -sdk [ExtJs6.0檔案夾地址] generate app [項目名稱] [項目路徑]      

      例如:sencha -sdk ext-6.0.0 generate app App MyApp

  (注):還可以加--classic或--modern建立pc或WAP單獨項目

二、在瀏覽器開啟

  命令列輸入:sencha app watch

三、開始寫login頁

  1、在view檔案夾中建立login檔案夾,在login中建立檔案login.js和loginController.js(login.js放在classic/view/login,loginController.js放在app/view/login)

  2、在app.js中禁用  mainView: ‘FirstTest.view.main.Main‘

  3、在login.js中寫頁面

    ①建立視窗  ②寫依賴、配置  ③寫登入的表單和按鈕

Ext.define(‘FirstTest.view.login.login‘, {    extend: ‘Ext.window.Window‘,    xtype: ‘login‘,    requires: [        ‘Ext.form.Panel‘,        ‘FirstTest.view.login.loginController‘    ],    controller:‘login‘,    bodyPadding: 10,    title:‘使用者登入‘,    closable:false,//視窗是否可關閉    autoShow:true,//windows預設是隱藏,要設定為顯示    items: {        xtype:‘form‘,        reference: ‘form‘,        items: [{            xtype:‘textfield‘,            name: ‘username‘,            fieldLabel: ‘使用者名稱‘,            allowBlank: false        },{            xtype:‘textfield‘,            name: ‘password‘,            fieldLabel: ‘密碼‘,            allowBlank: false        }],        buttons: [{            text:‘登入‘,            formBind: true,//按鈕是否可用取決於form            listeners:{                click: ‘onLoginClick‘            }        }]    }});

  4、在loginController.js中寫登入按鈕的onLoginClick事件

    ①在localStorage中記錄登入狀態(寫入TutorialLoggedIn:true)  ②destroy登入頁  ③create首頁

Ext.define(‘FirstTest.view.login.loginController‘,{    extend:‘Ext.app.ViewController‘,    alias:‘controller.login‘,    onLoginClick: function() {        // Set the localStorage value to true        localStorage.setItem("TutorialLoggedIn", true);        // Remove Login Window        this.getView().destroy();        // Add the main view to the viewport        Ext.create({            xtype: ‘app-main‘        });    }})

 

  5、添加啟動邏輯到Application.js(app/Application.js)

  ①判斷是否登入(通過判斷localStorage中的TutorialLoggedIn是否存在),若登入則開啟首頁,否則開啟登入頁

Ext.define(‘FirstTest.Application‘, {    extend: ‘Ext.app.Application‘,        name: ‘FirstTest‘,    stores: [        // TODO: add global / shared stores here    ],    views: [        ‘FirstTest.view.login.login‘,        ‘FirstTest.view.main.Main‘    ],        launch: function () {        // TODO - Launch the application        var loggedIn;        // Check to see the current value of the localStorage key        loggedIn = localStorage.getItem("TutorialLoggedIn");        // This ternary operator determines the value of the TutorialLoggedIn key.        // If TutorialLoggedIn isn‘t true, we display the login window,        // otherwise, we display the main view        Ext.create({            xtype: loggedIn ? ‘app-main‘ : ‘login‘        });    },    onAppUpdate: function () {        Ext.Msg.confirm(‘Application Update‘, ‘This application has an update, reload?‘,            function (choice) {                if (choice === ‘yes‘) {                    window.location.reload();                }            }        );    }});

  

  6、在main.js中添加Viewport外掛程式

plugins: ‘viewport‘,

   這個不加,登入後就是一片空白。

--------------------------------------------------到這裡,整個登入就寫好啦。下面寫一下怎麼登出。-------------------------------------------------------------------------------------------------------------

四、登出

  1、寫一個登出按鈕

{    xtype:‘button‘,    text:‘登出‘,    iconCls:‘x-fa fa-power-off‘,    handler: ‘onClickButton‘}

  2、在MainController.js中寫登出的方法onClickButton

  onClickButton: function () {        // Remove the localStorage key/value        localStorage.removeItem(‘TutorialLoggedIn‘);        // Remove Main View        this.getView().destroy();        // Add the Login Window        Ext.create({            xtype: ‘login‘        });    },

 

到此,登入登出就都寫好了。

 

用extjs6.0寫一個系統登入及登出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.