PDA

View Full Version : How to make listeners work in class using Observable mixin



PulpMysteryFan
30 Oct 2011, 8:28 AM
What's wrong with my syntax? I want to create an instance using the "listeners" config, but it only works when using the on() method. Thanks! :-)


Ext.define('Test.model.Pinger', {
extend : 'Ext.Base',
mixins : [ 'Ext.util.Observable' ],
constructor : function(config) {
config = config?config:{};
this.initConfig(config);
this.addEvents('ping');
this.listeners = config.listeners;
return this.callParent(arguments);
},
ping : function() {
this.fireEvent('ping', this);
}
});


Ext.onReady(function() {
var pinger = Ext.create('Test.model.Pinger');
pinger.on('ping', function(p) {
// this IS getting run
alert('it pinged using on()');
});
pinger.ping();
pinger = Ext.create('Test.model.Pinger', {
listeners : {
ping : function(p) {
// this is NOT getting run
alert('it pinged using listeners')
}
}
});
});

skirtle
30 Oct 2011, 10:55 AM
If you take a look at the source for AbstractComponent you'll see this line:


me.mixins.observable.constructor.call(me);

http://docs.sencha.com/ext-js/4-0/source/AbstractComponent.html#Ext-AbstractComponent

I don't see anything comparable in your code.

PulpMysteryFan
31 Oct 2011, 6:43 AM
How does one know when it's necessary to call the mixin's constructor? (And could this be automated by the class system?)

mitchellsimoens
31 Oct 2011, 7:17 AM
How does one know when it's necessary to call the mixin's constructor? (And could this be automated by the class system?)

Calling the constructor (e.g. instantiating the mixin) where you want gives you, the developer, control of where you want that mixin to be created. Maybe you want to do some application logic before a certain mixin is created so that mixin has something it needs. Just kind of hard to assume where devs wants things done.