PDA

View Full Version : Ext.Ajax.request processing data.



joao_candido
25 Mar 2010, 8:17 AM
I´ve got an ajax request which get an html code as result and my problem is.
When in such code has javascript and it isn´t loaded/executed.

Is there anything in Extjs which can be used to get this javascript loaded/executed?

Thanks in advance.

Animal
25 Mar 2010, 8:20 AM
But why would you load HTML in an Ext app?

joao_candido
25 Mar 2010, 8:22 AM
Let´s explain a little bit.

My system works on PHP, MySql and Smarty.

So, on each link I use Extjs.Ajax.request and get an HTML code as response to uptade my system screen. So, sometimes I need to pass som javascript code in it.

Thanks.

Animal
25 Mar 2010, 8:53 AM
So you are forced to load HTML instead of creating your UI by instantiating ExtJS Components?

mschwartz
25 Mar 2010, 8:55 AM
If the application is editing (directly, visually, HTMLEditor) HTML content and then previewing it (via smarty for example), then an iframe is in order.

joao_candido
25 Mar 2010, 9:03 AM
This is more or less what I´m talking about.



Ext.Ajax.request({
url: "includes/anteriores.php",
method: "POST",
params: {
"grupo": Ext.get("grupo").getValue(),
"segmento": Ext.get("segmento").getValue()
},
failure: function (response) {
Ext.Msg.Alert("Erro", "Erro na solicitação!<br>"+response.responseText);
},
success: function (response) {
Ext.get("content").dom.innerHTML = response.responseText;
}
});


In the above response.responseText I can have some javascript code which is no loaded.
In an old system of mine, I´ve got an external function which reads the response.responseText, get all scripts in it and add it into the dom object.

I´d just like to know if Extjs has any function/method which can do it.

Thanks.

joao_candido
29 Mar 2010, 8:49 AM
My solution for this case was the follow functions:



Ext.getScript = function (text) {
Ext.getScriptFile(text);
text = text.replace(/\r|\n/g, "");
var pattern = /<script[^>]*>.*?<\/script>/g;
var scripts = text.match(pattern);
if (scripts !== null) {
var script = null;
var txtScript = "";
for (s = 0; s < scripts.length; s++) {
txtScript = scripts[s].replace(/<script[^>]*>/, "");
txtScript = txtScript.replace(/<\/script>/, "");
if (txtScript.length > 0) {
script = document.createElement("scr"+"ipt");
script.type = "text/javascript";
script.text = txtScript;
document.body.appendChild(script);// document.body.appendChild(script);
}
}
}
}

Ext.getScriptFile = function (text) {
var pattern = /includes\/js[^'\"> ]*/g;
var scripts = text.match(pattern);
var script = null;
if (scripts !== null) {
for (s = 0; s < scripts.length; s++) {
script = document.createElement("scr"+"ipt");
script.type = "text/javascript";
script.src = scripts[s];
document.body.appendChild(script);// document.body.appendChild(script);
}
}
}


So, in the success or in the callback function I just call Ext.getScript giving it the responseText.

Thanks guys.

Animal
29 Mar 2010, 10:35 AM
Ext can do that. If you read the docs. But its bad practice

joao_candido
29 Mar 2010, 10:46 AM
That was exactly my question. Wheter Ext does it or not.