Coming from a C++ background, I'm getting a little lost in Javascript/ExtJS function scoping - specifically when dealing with events.
Say I have a controller which listens in on many different events. These events could come from managed views, stores, application-level, ect. Currently, I'm doing something like this to ensure correct scoping:
init: function() {
me.control({
'someSelector': {
someEvent: me.onSomeEvent.bind(me)
},
...
});
me.application.on({
applicationEvent: me.onApplicationEvent.bind(me)
});
},
onSomeEvent: function() {...},
onApplicationEvent: function() {...},
Is this the correct way of going about things? I'm scared of using this
inside onSomeEvent()
and onApplicationEvent
unless I know specifically what this
is pointing to. Thoughts?
edit: here's some example code to show where this
may be misleading.
init: function() {
this.control({
'uploadform *': {
formUpload: this.onFormUpload
}
});
},
onFormUpload: function(form) {
if(form.isValid()) {
var formData = form.getValues();
Ext.Ajax.request({
url: 'upload',
method: 'POST',
jsonData: {
form: formData
},
success: this.onFormUploadSuccess // here, this is the controller
});
}
},
onFormUploadSuccess: function() {
... // here, this isn't the controller
this.application.fireEvent('dealAdded'); // this.application === undefined!!
}