PDA

View Full Version : Call Controller Method from outside Ext namespace



rfox
29 Nov 2011, 9:34 AM
Hello, Got an MVC app with a google maps panel(https://github.com/VinylFox/ExtJS.ux.GMapPanel) the map has a few info windows that load files from my web server.

These files have some data and a button/link. When the link is clicked I would like to somehow call a function within an ext controller to activate some ext based behavior.

I've done this in a basic manner by doing:


Ext.get('button').on('click', function() { controller.method();});

within the controller, to do this i need to workout when the info window is loaded and then call the above code.

I was wondering if there is any other way to do this that might be a bit easier?

mitchellsimoens
29 Nov 2011, 11:13 AM
In the GMapPanel.js that is bundled with Ext JS 4 (that Shea created), when you add a marker, you can set the listeners config on the marker and it will be listened on.

rfox
29 Nov 2011, 11:47 AM
This is not the issue, Sorry if i wasn't clear.

I can get the marker's info window appearing, I need to call ext controller from inside the info windows html, this is loaded from a separate file and displayed.

To re clarify from a different angle:

How would I access a controller method from the console? eg. Ext.getController('BLAH').method()

If this isnt possible how could i go about doing this?

solodesignz
29 Nov 2011, 11:57 AM
What about:

AppName.controller.ControllerName.prototype.methodToCall()

solodesignz
29 Nov 2011, 12:12 PM
Well, that lets you call a function from inside the controller. But, it seems like its not the "full" controller.
For example, I'm trying to reload a grid's store from a custom function inside the controller:



gridRefresh: function () {
store = this.getUsersStore();
store.reload();
}


Calling via: AppName.controller.Users.prototype.gridRefresh()

Well, this calls the function correctly, but the controllers built in methods like: getUsersStore() are undefined.

Anyone have any input?

rfox
29 Nov 2011, 12:17 PM
Problem there is your not getting an active instance of the class :(.

solodesignz
29 Nov 2011, 12:18 PM
Yeah =/ I think i'm on to something, will let you know here in a few

solodesignz
29 Nov 2011, 12:22 PM
Damn, thought this might work: http://www.sencha.com/forum/showthread.php?133931

With listeners, but seems like its the same results... as the other one

solodesignz
29 Nov 2011, 12:38 PM
Figured it out for my instance, using a global var:



var userController = null;
Ext.define('RC.controller.Users', {
init: function () {
userController = this.getController('Users');
},
gridRefresh: function () {
store = this.getUsersStore();
store.load();
}
});



userController.gridRefresh();

rfox
1 Dec 2011, 3:34 AM
Oo, that would work however it breaks a lot of the encapsulation that ext has. Im now looking to see if i can call application.fireEvent from outside as this makes more sense.

If this doesn't work ill use your method and come back to it later thanks :D

I opted for a more controlled approach that doesn't expose my controllers(theres probably an easier way to do this but meh it works):

Above the app declaration in app.js


var App;
function App(){
var app;
this.fireEvent = function(evt,opts){
app.fireEvent(evt, opts);
}
this.setApp = function(a){
app = a;
}
}
Inside the apps launch or init:


App = new App();
App.setApp(this);

Then from the console you can do:


App.fireEvent()