简体   繁体   English

在sencha touch中使用按钮导航

[英]Navigating with a button in sencha touch

I am taking my first steps with Sencha touch. 我正在采取Sencha touch的第一步。 The results are just what I am after, getting there however is a struggle to get how sencha is put together. 结果正是我所追求的,然而到达那里却很难得到如何将sencha放在一起。 I am slowly figuring it out but sometimes the way the code works is a bit WTF. 我正在慢慢搞清楚,但有时代码的工作方式有点WTF。

I am trying to build a very simple app that does the following. 我正在尝试构建一个非常简单的应用程序,执行以下操作。

1) Has three tabs, Search nearby (geo), Quick Keyword Search, Category Search. 1)有三个选项卡,搜索附近(地理位置),快速关键字搜索,类别搜索。
2) Each of the tabs search returns a list of results 2)每个选项卡搜索都返回结果列表
3) Each of the rows are click able to show a bit more information. 3)每个行都可以点击,以显示更多信息。

I have figured out the tabs okay I think. 我已经想出了我认为的标签。

Like so: 像这样:

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
            ]
        });
    }
});

That works great. 这很好用。 No difference between the tabs I know but I'm building up to that... 我知道的标签之间没有区别,但我正在建立...

Right, the first thing I want to work on is the Keyword search tab as that is the simplest one to test for this app. 是的,我想要处理的第一件事是关键字搜索选项卡,因为这是测试此应用程序最简单的选项。

So I add a form. 所以我添加了一个表单。

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);

So my quick tab config now looks like this: 所以我的快速标签配置现在看起来像这样:

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

I now have a form looking exactly how I want and hooked into my json search and returning adverts to the console. 我现在有一个表格,看看我想要的内容,并挂钩到我的json搜索并将广告返回到控制台。 Great! 大!

Now I want to create a list view that has a top bar with a back button. 现在我想创建一个列表视图,其顶部栏带有后退按钮。 This I am pretty sure is not the way to set this up, but I am really struggling to find examples on how to do this as the example with the source have a complicated setup, and the simple ones don't do what I am after. 我很确定这不是设置它的方法,但我真的很难找到如何做到这一点的例子,因为源代码的示例有一个复杂的设置,而简单的那些不做我以后做的事情。

I now add a model config at the top of my index.js file to define my Advert model 我现在在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'}
    ]
});

In my form success listener I do the following: 在我的表单成功听众中,我执行以下操作:

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));
                }
        }

This works however it doesn't slide in as I would like, as it does when clicking the icons in the bottom tab bar. 但是它可以按照我的意愿滑入,就像点击底部标签栏中的图标一样。

Now this is where I fall down, I can't figure out how I make the back button work to take me back to the keyword search. 现在这是我倒下的地方,我无法弄清楚我如何使后退按钮工作,让我回到关键字搜索。

I have found setCard and setActiveItem but I don't seem able to access those in the "this" context in the result listener function. 我找到了setCard和setActiveItem,但我似乎无法在结果监听器函数中访问“this”上下文中的那些。

Could someone give a simple example of how to do this? 有人可以给出一个如何做到这一点的简单例子吗?

The easiest way to solve this is probably by giving your TabPanel an id and then referencing it inside your handler. 解决这个问题的最简单方法可能是给你的TabPanel一个id,然后在你的处理程序中引用它。 Try updating your tabpanel like this: 尝试更新您的tabpanel,如下所示:

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

And your back button handler like this: 你的后退按钮处理程序如下:

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

This will move to the first card in the tabpanel (your location card). 这将移至tabpanel中的第一张卡(您的位置卡)。

Alternatively, if you want to modify the value of 'this' in the handler function, you can just pass in a scope: 或者,如果要在处理函数中修改'this'的值,则只需传入作用域:

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

'this' now refers to whatever you passed in on the scope config. 'this'现在指的是你在范围配置中传入的任何内容。 It's very common to see people use "scope: this" so that 'this' inside their handler is the same as 'this' outside the handler. 看到人们使用“scope:this”是很常见的,因此处理程序中的“this”与处理程序外部的“this”相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM