PDA

View Full Version : How to solve the case when a button's handler is received from PHP server side?



david.appeltans
5 Jan 2013, 3:17 AM
Dear all,
I currently have a site in which the user rights are managed on the server side and my application menu is dynamically build on the server side (PHP).
When the menu is build, it defines which javascript function should be assigned to the menu buttons.
The issue is that the PHP send the menu to create in JSON format to the client.
In JSON all the key value pairs need to be double quoted.
On the client site this means that my button action handlers come in as a string (surrounded with double quotes in the JSON) and I cannot execute the functions.
In order to solve this, I changed the extjs 4.1 source code in the following way:
FROM:
======

Ext.define('Ext.button.Button', {
...
fireHandler: function(e){
var me = this,
handler = me.handler;

if (me.fireEvent('click', me, e) !== false) {
if (handler) {
handler.call(me.scope || me, me, e);
}
me.blur();
}
...

TO:

Ext.define('Ext.button.Button', {

fireHandler: function(e){
var me = this,
handler = me.handler;

if (me.fireEvent('click', me, e) !== false) {
if (handler) {
if (typeof(me.handler) == "string") {
eval(me.handler);
} else {
me.handler.call(me.scope || me, me, e);
}
}
me.blur();
}


My dynamic menu generation on the server side works with this modification.
However I think that there is maybe a better way to do it as doing an eval is not secure and I don't like to modify directly the source of extjs 4.1.
For this reason I have the following questions:
1. What is the advised way to change the default button handler function in Extjs 4.1?
2. Is there any other way of achieving the same result without having to modify the default Ext source?
3. In case I need to modify the extjs source code, will I be able to still use sencha command line tools to create a production build but using my "own" ext-all-debug.js source code?

Thanks in advance for your help.

Kind regards,
David

mitchellsimoens
7 Jan 2013, 10:52 AM
First, you should NEVER modify the Ext JS source (maintainability is out the door if you do that). You should either extend or override the class.

Have you looked at Ext.ComponentLoader?