Extjs Sample Login App: TutorialApp

1. 使用SenchaCmd自动生成app

$ sencha -sdk /home/ext-6.0.0 generate app -classic TutorialApp ./TutorialApp

    app生成后路径: /home/jalen_chu/TutorialApp

2. 创建登陆页面:

    1)修改:进入 /TutorialApp/app/view 创建如下

    18092853_Jcla.jpeg

    2)Login.js

Ext.define('TutorialApp.view.login.Login',{
    extend:'Ext.window.Window',
    xtype:'login',

    requires:[
        'TutorialApp.view.login.LoginController',
        'Ext.form.Panel'
    ],

    controller:'login',
    bodyPadding:10,
    title:'Login Window',
    closable:false,
    autoShow:true,

    items:{
        xtype:'form',
        reference:'form',
        items:[{
            xtype:'textfield',
            name:'username',
            fieldLabel:'Username',
            allowBlank:false
        },{
            xtype:'textfield',
            name:'password',
            inputType:'password',
            fieldLabel:'Password',
            allowBlank:false
        },{
            xtype:'displayfield',
            hideEmptyLabel:false,
            value:'Entry any non-blank password'
        }],
        buttons:[{
            text:'Login',
            formBind:true,
            listeners:{
                click:'onLoginClick'
            }
        }]
    }
});

    3)LoginController.js

Ext.define('TutorialApp.view.login.LoginController',{
    extend:'Ext.app.ViewController',
    alias:'controller.login',

    onLoginClick:function(){
    
        // This would be the ideal location to verify the user's credentials via
            // a server-side lookup. We'll just move forward for the sake of this example.

            // Set the localStorage value to true
        localStorage.setItem("TutorialLoggedIn",true);
        
        // Remove Login Window
        this.getView().destory();

        // Add the main view to the viewport
        Ext.create({
            xtype:'app-main'
        });
    }
});

3. 创建主页面内容:

    直接修改 /TutorialApp/app/view/main 目录下Main.js 和MainController.js 文件

    1)Main.js

/**
 * This class is the main view for the application. It is specified in app.js as the
 * "mainView" property. That setting automatically applies the "viewport"
 * plugin causing this view to become the body element (i.e., the viewport).
 *
 * TODO - Replace this content of this view to suite the needs of your application.
 */
Ext.define('TutorialApp.view.main.Main', {
    extend: 'Ext.tab.Panel',
    xtype: 'app-main',

    requires: [
        'Ext.plugin.Viewport',
        'Ext.window.MessageBox',

        'TutorialApp.view.main.MainController',
        'TutorialApp.view.main.MainModel',
        'TutorialApp.view.main.List'
    ],

    controller: 'main',
    viewModel: 'main',
    plugins:'viewport',

    ui: 'navigation',

    tabBarHeaderPosition: 1,
    titleRotation: 0,
    tabRotation: 0,

    header: {
        layout: {
            align: 'stretchmax'
        },
        title: {
            bind: {
                text: '{name}'
            },
            flex: 0
        },
        iconCls: 'fa-th-list',
    items:[{
        xtype:'button',
        text:'Logout',
        margin:'100',
        handler:'onClickButton'
    }]
    },

    tabBar: {
        flex: 1,
        layout: {
            align: 'stretch',
            overflowHandler: 'none'
        }
    },

    responsiveConfig: {
        tall: {
            headerPosition: 'top'
        },
        wide: {
            headerPosition: 'left'
        }
    },

    defaults: {
        bodyPadding: 20,
        tabConfig: {
            plugins: 'responsive',
            responsiveConfig: {
                wide: {
                    iconAlign: 'left',
                    textAlign: 'left'
                },
                tall: {
                    iconAlign: 'top',
                    textAlign: 'center',
                    width: 120
                }
            }
        }
    },

    items: [{
        title: 'Home',
        iconCls: 'fa-home',
        // The following grid shares a store with the classic version's grid as well!
        items: [{
            xtype: 'mainlist'
        }]
    }, {
        title: 'Users',
        iconCls: 'fa-user',
        bind: {
            html: '{loremIpsum}'
        }
    }, {
        title: 'Groups',
        iconCls: 'fa-users',
        bind: {
            html: '{loremIpsum}'
        }
    }, {
        title: 'Settings',
        iconCls: 'fa-cog',
        bind: {
            html: '{loremIpsum}'
        }
    }]
});

    2)MainController.js

/**
 * This class is the controller for the main view for the application. It is specified as
 * the "controller" of the Main view class.
 *
 * TODO - Replace this content of this view to suite the needs of your application.
 */
Ext.define('TutorialApp.view.main.MainController', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.main',

    onItemSelected: function (sender, record) {
        Ext.Msg.confirm('Confirm', 'Are you sure?', 'onConfirm', this);
    },

    onConfirm: function (choice) {
        if (choice === 'yes') {
            //
        }
    },

    onClickButton:function(){
    // Remove the localStorage key/value
    localStorage.removeItem("TutorialLoggedIn");
    
    // Remove Main View
    this.getView().destory();

    // Add the Login Window
    Ext.create({
        xtype:'login'
    });
    }
});

4. 业务逻辑:登陆后进入主页面,退出后返回登陆页面

    修改 /TutorialApp/app/Application.js 

/**
 * The main application class. An instance of this class is created by app.js when it
 * calls Ext.application(). This is the ideal place to handle application launch and
 * initialization details.
 */
Ext.define('TutorialApp.Application', {
    extend: 'Ext.app.Application',
    
    name: 'TutorialApp',

    stores: [
        // TODO: add global / shared stores here
    ],
    
    views: [
    'TutorialApp.view.login.Login',
    'TutorialApp.view.main.Main'
    ],

    launch: function () {
        // TODO - Launch the application
    // It's important to note that this type of application could use
        // any type of storage, i.e., Cookies, LocalStorage, etc.
    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();
                }
            }
        );
    }
});

5. 运行:

$ cd /home/jalen_chu/TutorialApp
$ sencha app watch

    浏览器:http://localhost:1841/ 

转载于:https://my.oschina.net/chumingcheng/blog/616765

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Uncaught TypeError: r is undefined" 错误是 JavaScript 中常见的类型错误。它通常表示在代码中尝试访问或操作一个未定义或为空的变量或属性。 对于 ExtJS,这个错误可能有多种可能的原因,以下是一些常见的情况和解决方法: 1. 检查变量或对象是否已定义:确保你使用的变量或对象已经正确地定义和初始化。如果你在使用某个变量之前没有为其赋值,就会出现此错误。在报错的地方打印相关的变量或对象,检查其是否为空或未定义。 2. 确保正确加载 ExtJS 库:确保你正确地加载了 ExtJS 库文件,并且在代码中使用之前已经成功加载。检查浏览器的开发者工具(如 Chrome 的开发者工具)中的网络面板,确认 ExtJS 文件是否成功加载。 3. 检查方法或属性是否存在:如果报错指向某个方法或属性调用,确保该方法或属性存在于相应的对象中。在调用方法之前,最好检查该方法是否存在,以避免报错。 4. 确保正确的版本和兼容性:如果你使用的是较新的 ExtJS 版本,而代码是基于较旧版本编写的,可能会导致不兼容或未定义的错误。确保你使用的 ExtJS 版本与你的代码兼容,并参考 ExtJS 的文档来了解可能的更改或更新。 5. 检查代码逻辑和语法错误:检查你的代码是否存在其他逻辑或语法错误,这些错误可能会导致错误的变量引用或未定义的属性。 如果以上方法都没有解决问题,我建议你提供更多的上下文和相关代码,以便更详细地分析和解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值