PDA

View Full Version : What's the best way to add helper method for controllers?



reynardmh
21 Sep 2011, 3:35 PM
I'm thinking maybe using namespace.



Ext.ns('Ctrl.test')
Ctrl.test.help = function() {};

Ext.regController("test", {
index: function(o) {
Ctrl.test.help();
}
});



Is there a better way to do this?

mitchellsimoens
22 Sep 2011, 5:16 AM
What are you trying to accomplish? I would say what you are trying to do is a little dirty. I would just put the function within the controller. Then within any methods of that controller you can still execute that function you added, the scope of the methods is to the controller so you can do this:


Ext.regController("app.controllers.test", {
index: function(o) {
this.help();
},

help: function() {...}
});

reynardmh
22 Sep 2011, 8:24 AM
Thanks, yeah that's what I need.
Is there a way to call it from other controller tho?

mitchellsimoens
22 Sep 2011, 8:29 AM
Thanks, yeah that's what I need.
Is there a way to call it from other controller tho?


app.getController('controller name').help();

Scope will probably be the current controller not the one you did the getController for. If it is, then you would have to do something like:


var controller = app.getController('controller name');
Ext.Function.bind(controller.help, controller)();

reynardmh
22 Sep 2011, 9:01 AM
Fantastic :) thanks again!