简体   繁体   English

Sencha事件处理和视图操作

[英]Sencha event handling and view manipulation

So I'm trying to wrap my head around the MVC design patter used in Sencha Touch. 因此,我试图围绕Sencha Touch中使用的MVC设计模式进行研究。 I have my main view 我有主见

//-- views/Main.js
Ext.define('PlanningTool.view.Main', {
  extend: 'Ext.Container',
  xtype: 'main',

  config: {
    layout:'vbox',
    items:[
    {
      html:'<p><span class="title-text">Door Weight Estimator &amp; Parts Calculator</span><img class="logo" src="resources/images/logo.png" /></p>',
      cls:'title'
    },
    {
      xtype:'overview' //-- views/Overview.js
    },
    {
      xtype:'action' //-- views/action.js
    },
    {
      xtype:'breadcrumb' //-- views/breadcrumb.js
    }]
  }
});

So let's say inside views/Overview.js I have: 所以让我们说在views/Overview.js内部:

//-- views/Overview.js
Ext.define('PlanningTool.view.Overview', {
      extend: 'Ext.Panel',
      xtype: 'overview',
      config: {
            cls:'overview',
            flex:3,
            items: [
            {
                xtype:'button',
                text:'Start Estimator',
                ui:'action',
                cls:'start-button'
            }]
      }
    });

Basically when the start button is pressed, I want to update some text in the breadcrumb view. 基本上,当按下开始按钮时,我想更新breadcrumb视图中的一些文本。

I come from an appcelerator background and I would do this by adding a click event handler to the button firing a custom event and inside breadcrumb, I would listen to for the custom event and proceed to do whatever manipulations I need to do. 我来自appcelerator背景,可以通过向click按钮中添加一个click事件处理程序来触发自定义事件并在面包屑内部进行操作,我会监听自定义事件并继续执行我需要做的任何操作。

I just want to do this the right way before I start finding hack jobs 我只是想以正确的方式进行操作,然后再开始寻找黑客工作

The procedure I would follow here is: 我在这里遵循的过程是:

  1. Add a reference to your breadcrumb object in a controller. 在控制器中添加对面包屑对象的引用。
  2. Add an event handler (Controller Action) to the same controller that listens for the click (tap) on your start button. 将事件处理程序(“控制器操作”)添加到侦听“开始”按钮上的单击(轻击)的同一控制器。
  3. Take appropriate action in the event handler. 在事件处理程序中采取适当的措施。

Something like this for a controller: 像这样的控制器:

Ext.define('MyApp.controller.BreadcrumbController', {
    extend: 'Ext.app.Controller',

    refs: [
        {
            ref: 'breadcrumbs',
            selector: '#breadcrumbs'
        }
    ],

    init: function(application) {
        this.control({
           "#start-button": {
               click: this.onStartClicked
           }
        });
    },

    onStartClicked: function(button, e, eOpts) {
       var bc = this.getBreadcrumbs();

       // do whatever to the bc object to update, etc...

    }

});

Note that for this to work, you'll have to give ids to your components. 请注意,要使其正常工作,您必须为组件提供ID。 For example, when creating the breadcrumbs, you can do the following: 例如,在创建面包屑时,您可以执行以下操作:

{
  xtype:'overview',
  id: 'overview'
},
{
  xtype:'action',
  id: 'start-button'
}{
  xtype:'breadcrumb',
  id: 'breadcrumbs'
}

Then you should be good to go! 那你应该很好走!

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

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