代码之家  ›  专栏  ›  技术社区  ›  johnwards

使用Sencha Touch中的按钮导航

  •  10
  • johnwards  · 技术社区  · 14 年前

    我第一步要做的就是接触sencha。结果正是我所追求的,然而要想达到这个目标却很难理解森查是如何被整合在一起的。我正在慢慢地理解它,但有时代码的工作方式有点wtf。

    我正在尝试构建一个非常简单的应用程序来执行以下操作。

    1)有三个选项卡,搜索附近(geo),快速关键字搜索,类别搜索。
    2)每个选项卡搜索返回结果列表
    3)每一行都可以单击以显示更多信息。

    我想我已经弄清楚标签了。

    像这样:

    Ext.setup({
        tabletStartupScreen: 'tablet_startup.png',
        phoneStartupScreen: 'phone_startup.png',
        icon: 'icon.png',
        glossOnIcon: false,
        onReady: function() {
    
                    var location = new Ext.Container({
                iconCls: 'search', 
                title: 'Location Search',
                items: [new Ext.Component({
                    html: '<img src="images/gfb.gif" />'
                })]
            });
    
            var quick = new Ext.Container({
                iconCls: 'search', 
                title: 'Quick Search',
                scroll: 'vertical',
                items: [new Ext.Component({
                    html: '<img src="images/gfb.gif" />'
                })]
            });
    
            var category = new Ext.Component({
                iconCls: 'search', 
                title: 'Category Search',
                html: '<img src="images/gfb.gif" /><p>Category</p>'
            });
            var tabpanel = new Ext.TabPanel({
                fullscreen: true,
                tabBar: {
                    dock: 'bottom',
                    scroll: 'horizontal',
                    sortable: false,
                    layout: {
                        pack: 'center'
                    }
                },
                cls: 'card1',
                items: [
                    location,
                    quick,
                    category
                ]
            });
        }
    });
    

    效果很好。我知道的标签之间没有区别,但我正在逐步建立…

    是的,我想做的第一件事是关键字搜索标签,因为这是最简单的测试这个应用程序。

    所以我添加了一个表单。

    var quickFormBase = {
                    url: "../quicksearch.php?output=json",
                    items: [{
                       xtype: 'fieldset',
                       instructions: 'The keyword search is great if you know the name of the company you are looking for, or the particular service you need to find.<p><br />To narrow it down to an area include the town or county name in your query</p>',
                       defaults: {
                           required: false,
                           labelAlign: 'left'
                       },
                       items: [{
                               xtype: 'textfield',
                               label: 'Search',
                               name : 'inpquery',
                               showClear: true,
                               autoCapitalize : false
                           }]
                }],
                listeners : {
                    submit : function(form, result){
                console.log('results', result.SearchResults.MainResults.Advert);
                    },
                    exception : function(form, result){
                        console.log('failure', Ext.toArray(arguments));
                    }
                }
        };
    
    var quickForm = new Ext.form.FormPanel(quickFormBase);
    

    所以我的快速选项卡配置现在看起来如下:

    var quick = new Ext.Container({
                iconCls: 'search', 
                title: 'Quick Search',
                scroll: 'vertical',
                items: [new Ext.Component({
                    html: '<img src="images/gfb.gif" />'
                }),quickForm]
    });
    

    现在我有了一个表单,可以准确地查看我想要的内容,并将其链接到我的JSON搜索中,并将广告返回到控制台。伟大的!

    现在,我想创建一个列表视图,它有一个带有后退按钮的顶栏。我很确定这不是设置的方法,但是我真的很难找到关于如何设置的例子,因为源代码的例子有一个复杂的设置,而简单的例子并不能满足我的要求。

    我现在在index.js文件的顶部添加一个模型配置来定义我的广告模型。

    Ext.regModel('Advert',{
        fields: [
                 {name: 'advertid', type:'int'},
                 {name: 'Clientname', type:'string'},
                 {name: 'telephone', type:'string'},
                 {name: 'mobile', type:'string'},
                 {name: 'fax', type:'string'},
                 {name: 'url', type:'string'},
                 {name: 'email', type:'string'},
                 {name: 'additionalinfo', type:'string'},
                 {name: 'address1', type:'string'},
                 {name: 'address2', type:'string'},
                 {name: 'address3', type:'string'},
                 {name: 'postcode', type:'string'},
                 {name: 'city', type:'string'},
                 {name: 'Countyname', type:'string'},
                 {name: 'longitude', type:'string'},
                 {name: 'latitude', type:'string'}
        ]
    });
    

    在我的“成功倾听者”表中,我执行以下操作:

    listeners : {
                    submit : function(form, result){
    
                        var quickstore = new Ext.data.JsonStore({
                            model  : 'Advert',
                            data : result.SearchResults.MainResults.Advert
                        });
    
                        var listConfig = {
                                tpl: '<tpl for="."><div class="advert">{Clientname}</div></tpl>',
                                scope: this,
                                itemSelector: 'div.advert',
                                singleSelect: true,
                                store: quickstore,
                                dockedItems: [
                                                {
                                                    xtype: 'toolbar',
                                                    dock: 'top',
                                                    items: [
                                                        {
                                                            text: 'Back',
                                                            ui: 'back',
                                                            handler: function(){
                                                                //Do some magic and make it go back, ta!
                                                            }
                                                        }
                                                    ]
                                                }
                                            ]
                        };
                        var list = new Ext.List(Ext.apply(listConfig, {
                            fullscreen: true
                        }));
                    },
                    exception : function(form, result){
                        console.log('failure', Ext.toArray(arguments));
                    }
            }
    

    但是,它不会像我希望的那样滑入,就像单击底部选项卡栏中的图标时那样。

    现在这就是我摔倒的地方,我不知道如何使后退按钮工作,带我回到关键字搜索。

    我找到了setcard和setactiveitem,但我似乎无法在result listener函数的“this”上下文中访问它们。

    有人能举个简单的例子说明怎么做吗?

    1 回复  |  直到 14 年前
        1
  •  12
  •   Ed Spencer    14 年前

    解决这个问题的最简单的方法可能是给TabPanel一个ID,然后在处理程序中引用它。尝试如下更新选项卡面板:

    var tabpanel = new Ext.TabPanel({
        id: 'mainPanel',
        ... the rest of your config here
    

    您的后退按钮处理程序如下:

    handler: function() {
        Ext.getCmp('mainPanel').layout.setActiveItem(0);
    }
    

    这将移动到选项卡面板(您的位置卡)中的第一张卡。

    或者,如果要在handler函数中修改“this”的值,可以只传入一个范围:

    text: 'Back',
    ui: 'back',
    scope: tabpanel,
    handler: function(){
        this.layout.setActiveItem(0);
    }
    

    “this”现在指的是您在scope配置中传递的任何内容。很常见的情况是人们使用“scope:this”,这样处理程序内部的“this”与处理程序外部的“this”相同。