PDA

View Full Version : [SOLVED] Custom listeners



ctp
20 Oct 2010, 9:12 AM
Howdy,

I use this simple function for my change listener:


listeners: {
change: function(obj) {
tmpProp[obj.id] = this.getValue();
for (var record in tmpProp) {
console.log(record + ': ' + tmpProp[record]);
}
}
}tmpProp is a global Object() instance. I need this code dozens of times so I try to get the code out of my listener: {change: {}} block to reuse it everywhere I need it. I'm using an application class like described in http://www.sencha.com/learn/Tutorial:Application_Layout_for_Beginners but don't get the code running. Anyone has a hint how to define an external function for this listener?

Keylan
20 Oct 2010, 9:53 AM
What is 'this' referencing when calling this.getValue()?

If you define that function externally:



function changeListener (obj) {
tmpProp[obj.id] = this.getValue();
for (var record in tmpProp) {
console.log(record + ': ' + tmpProp[record]);
}

...

listeners: {
change: changeListener.createDelegate(this)
}

May get you what you are looking for..

ctp
20 Oct 2010, 10:22 AM
Many thanks for the prompt reply Keylan.

It seems, I'm in newbie trouble ;-) I wanna define changeListener in global context, so my main JS consists of:


Ext.BLANK_IMAGE_URL = '/ext/resources/images/default/s.gif';
Ext.ns('App', 'App.widget', 'App.store', 'App.var');

App.main = function() {

// private variables

// private functions

// public space
return {

// public properties

// public methods
init: function() {
App.var.tmpProperty = new Object();
function changeListener (obj) {
App.var.tmpProperty[obj.id] = this.getValue();
for (var record in App.var.tmpProperty) {
console.log(record + ': ' + App.var.tmpProperty[record]);
}
};
}
};
}();I followed the beginners howto: http://www.sencha.com/learn/Tutorial:Application_Layout_for_Beginners

I'm initializing the app by


Ext.onReady(App.main.init, App.main)... so all should work fine. My listener I'm calling


listeners: { change: changeListener.createDelegate(this) }

and get an error: "changeListener is not defined"

Hm, it seems the changeListener function is not visible by my further code, but can't see why ...

steffenk
20 Oct 2010, 10:28 AM
it's not global.

Change it to


App.changeListener = function() {...}

...

listeners: { change: App.changeListener.createDelegate(this) }

ctp
20 Oct 2010, 10:54 AM
Great, all works fine now 8-) Many, many thx ...

Keylan
20 Oct 2010, 10:59 AM
Hurray teamwork.