views:

372

answers:

3

Well basically im looking on this problem, i have many components with dinamic stuff that is written in the server side with PHP.

Depending on the user my components will change, based on the role of the user.

So i need to know any ways/examples/info on how to do this.

1- I used the load function EXTJS has, but it clearly says i wont load script only plain text.

2- i used eval() but im a bit scared o this approach, like this example crate layout component (static)

var contentPanel = new Ext.Panel({
                frame: true,
                style: {marginTop: '10px'},
                height: 315,
                border: true,
                bodyBorder: false,
                layout: 'fit',
                id: 'contentPanel'
            });


            var mainPanel = new Ext.Panel({
                title: 'Panel Principal',
                id: 'mainPanel',
                border: true,
                frame: true,
                width: '50%',
                style: {margin: '50px auto 0 auto'},
                height: 400,
                renderTo: Ext.getBody(),
                items: [
                        {
                            html: '<a href="#" onClick="requestContent(\'panel1\');">Panel 1</a>'
                        },
                        {
                            html: '<a href="#" onClick="requestContent(\'panel2\');">Panel 2</a>'
                        },
                        contentPanel

                ]
            })

and update the content of the layout with js files written on the server

function receiveContent(options, success, response)
        {



            var respuesta = response.responseText;

            //console.log(respuesta);

            eval(respuesta);

            //console.log(options.url);

            url = options.url;

            url = url.substring(0,(url.search(/(\.)/)));

            var contenedor = Ext.getCmp('contentPanel');

            contenedor.removeAll();

            var contenido = Ext.getCmp(url);

            contenedor.add(contenido);
            contenedor.doLayout();
        }

        function requestContent(panel)
        {
            //panel es el nombre del archivo que quiero
            Ext.Ajax.request({
                url: panel+'.js',
                callback: receiveContent
            });
        }

any other way for this to be done, what i DONT want to do is making a million different components and load them ALL at login time like many people seem to say

+2  A: 

You might load JavaScript dynamically using something like like below - there are a hundred variations on the web. In this way, you would avoid the AJAX call and handling the response (and subsequent eval).

var aHeadNode = document.getElementById('head')[0];
var aScript = document.createElement('script');
aScript.type = 'text/javascript';
aScript.src = "someFile.js";
aHeadNode.appendChild(oScript);
Upper Stage
Thanks for the Answer, Upper, but how do i know WHEN i can call the script added like this ? I mean, how do i know when the script has been loaded before using some variable/function defined in it ? is there any ON LOAD event available?
GumaTerror
There is an event. However, you may be looking in the wrong place. Put the onload handler in the file loaded rather than in the file loading.
Upper Stage
A: 

To address your questions:

  1. The .load method WILL load script and evaluate it once the content has finished loading, however to accomplish this you will need to set the scripts:true option, an example may be:

    my_panel.load({ url: 'url_to_load.php/hmt/html/asp...', params: {param1: param1value, param2: param2value...etc}, nocache: true, timeout: 30, scripts: true });

  2. Using eval() is fine...but seeing as the scripts:true config option above accomplishes this for javascript in the source file, you shouldnt need to use this.

Hope this helps

Ergo Summary
Sorry- not sure why the code tag isnt working for my post, meaning the formatting of the code isnt being shown correctly...
Ergo Summary
A: 
Swar