Sencha 2.x MVC: Get element by id

我的梦境 提交于 2019-12-08 09:15:53

问题


I just started using Sencha framework 2.x. This is my app:

app/app.js

Ext.Loader.setConfig({
  enabled: true
});
Ext.application({
  name: 'App',
  controllers: ['Generators'],
  models: [],
  stores: [],
  views: ['Main', 'Generator'],
  launch: function() {
    Ext.create('App.view.Main');
  }
});

app/view/Main.js

Ext.define('App.view.Main', {
  extend: 'Ext.NavigationView',
  requires: [
    'App.view.Generator'
  ],

  config: {
    fullscreen: true,
    items: [
      {
        xtype: 'generatorview'
      }
    ]
  }
});

app/view/Generator.js

Ext.define('App.view.Generator', {
  extend: 'Ext.Container',
  xtype: 'generatorview',
  id: 'generator',
  config: {
    layout: 'vbox',
    items: [
      {
        xtype: 'panel',
        html: 'My message: <a id="xxxSet">Set</a> :: <span id="xxxMsg">...</span>',
        flex: 1
      },
      {
        dock: 'bottom',
        xtype: 'toolbar',
        items: []
      }
    ]
  }
});

app/controller/Generator.js

Ext.define('App.controller.Generators', {
  extend: 'Ext.app.Controller',
  config: {
    refs: {}
  },
  init: function() {
    this.control({
      '#xxxSet': { // QUESTION1
        tap: 'setMyMessage'
      }
    });
  },

  setMyMessage: function() {
    '#xxxMsg'.html('Set this message'); // QUESTION2
  }
});

As you can see I placed questions in the last part (controller).

  • QUESTION1: How can I set a tap function to the element (#xxxSet) defined in the view as HTML content.
  • QUESTION2: How can I set a message the the element (#xxxMsg) defined in the view as HTML content.

xxxSet = id of a button

xxxMsg = id of a message holder

Thx!


回答1:


You can use Ext#get (which accepts a string which is the id) which will return a instance of Ext.dom.Element. With that, you can use the on method to add listeners (much like control) and then the setHtml method to update the contents.

init: function() {
    Ext.get('xxxSet').on({
        tap: 'setMyMessage',
        scope: this
    });
},

setMyMessage: function() {
    Ext.get('xxxMsg).setHtml('Hello');
}



回答2:


If you use itemId, you can not access it with Ext.get(). You can try Ext.ComponentQuery.query() instead of that.

init: function() {
Ext.ComponentQuery.query('#xxxSet').on({
    tap: 'setMyMessage',
    scope: this
  });
},

setMyMessage: function() {
 Ext.ComponentQuery.query('#xxxMsg).setHtml('Hello');
}


来源:https://stackoverflow.com/questions/9481597/sencha-2-x-mvc-get-element-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!