標籤: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寫一個系統登入及登出