Search code examples
javascripteventsextjsconstructorextjs4

ExtJS addEvents - is it optional?


Using an ExtJS example from http://www.extjs-tutorial.com/extjs/custom-events-in-extjs

Can someone explain why it makes no difference if I comment those 2 lines in the constructor as bellow?

Here is the code:

Ext.define('Student', {
 config : {
    name : '',
    schoolName : ''
 },

 mixins : 
 {
    observable : 'Ext.util.Observable'
 },

 constructor : function(config){
    // this.addEvents('studentNameChanged');       
    this.mixins.observable.constructor.call(this, config);
    // this.initConfig(config);
 },

 updateName : function(newValue, oldValue){
    this.fireEvent('studentNameChanged', newValue);
 }
});

var newStudent = Ext.create('Student', { name: 'xyz' });
newStudent.on('studentNameChanged', function(name){
  alert('student Name has been Chaged to ' + name);
});
newStudent.setName('John');

Solution

  • IIRC using addEvents was mandatory in Ext JS 3.x and below, was deprecated in 4.x and will produce an error in 5.0+. Don't use it.