The referenced fiddle and code here does not actually work. I don't know how to get a controller defined in a one file project. At any rate, my issue is that when I'm in a controller function (afterrender in my case). I can't figure out how to get a reference to testval. if I create a simple example with just lines 19-32 in a separate launch it works (see fiddle: https://fiddle.sencha.com/#fiddle/1ae7 ).
However, in my controller case I'm not getting the testval in my success or failure event. how to pass in context (scope) of my function I'm calling the ajax request from?
https://fiddle.sencha.com/#fiddle/1ae9
Ext.define('mycontroller', {
extend: 'Ext.app.Controller',
alias: 'controller.main',
myfun: function() {
console.log('controller:myfun');
}
});
var mypanel = Ext.create('Ext.panel.Panel', {
controller: {
type: 'main'
},
listeners: {
afterrender: function() {
console.log('afterrender');
var testval = "hi there";
Ext.Ajax.request({
method: 'POST',
url: '/dummy',
jsonData: 'jsonData',
scope: this,
success: function() {
// this never gets called, just testing scope
},
failure: function() {
// testval IS NOT IN SCOPE, CONFUSED ABOUT SCOPE AND THIS HERE
Ext.Msg.alert('info', testval);
}
});
}
},
html: 'test panel'
});
Ext.application({
name: 'Fiddle',
launch: function() {
Ext.create('Ext.container.Viewport', {
layout: 'fit',
items: [mypanel]
});
}
});
testvaldefined insideafterrender, like in your example? Then it should work, controller is not used in your example anywhere,afterrenderbelongs to the view and you are accessing some local scope variable inside a function. if you want to move the listener to a controller you need to define the listener aslisteners: {afterrender:"controller_function"}, and move your function to the controller file. - sergExt.app.ViewController. Once you fix that up it runs and shows the failure message. Otherwise, it's just JS, thetestvalvar is captured in closure scope. - Evan Trimboli