xpepermint
xpepermint

Reputation: 36263

Sencha 2.x MVC: Get element by id

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

xxxSet = id of a button

xxxMsg = id of a message holder

Thx!

Upvotes: 0

Views: 5886

Answers (2)

SaliproPham
SaliproPham

Reputation: 163

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

Upvotes: 0

rdougan
rdougan

Reputation: 7225

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

Upvotes: 2

Related Questions