PDA

View Full Version : override Ext.onReady



alupuli
12 Apr 2010, 1:55 PM
trying to override(?) Ext.onReady

myNamespace.onReady=Ext.EventManager.onDocumentReady;


myNamespace.onReady(function () {
//all UI building code

});

Want to execute some fixed custom functions before firing the listener passed.

myNamespace.onReady() will be called from different pages. Want to make sure some fixed code is executed before firing the listener.

Is it possbile to do without modifying any ext source files.

alupuli
12 Apr 2010, 3:29 PM
got the solution which I looked for.

myNamespace.onReady = function (a) {
var f = function () {
//some custome code
a();
}
Ext.EventManager.onDocumentReady(f);
}

any better ways?

steffenk
12 Apr 2010, 3:31 PM
What you try is not needed. Just do your stuff before starting your app.

alupuli
12 Apr 2010, 8:10 PM
requirement was to dynamicaly load external javascript language file before firing the listener.



myNamespace.onReady = function (a) {
var f = function () {
var language = //read language from cookie
Ext.Ajax.request({
url: "external_" + language + ".js",
success: function () {
eval(responseText);
a();
}
}

}
Ext.EventManager.onDocumentReady(f);
}

myNamespace.onReady(function ()
//UI building
});

Animal
13 Apr 2010, 10:22 AM
Steffen was correct. Anyway, all you are doing is what he says.

Your code executes before the listener is added

So what you have done is pointless.

alupuli
25 Apr 2010, 7:41 PM
Animal,
sorry, I am not clear..
as you can see code in previous reply, I am trying to load language js dynamically before UI building code.
So using

myNamespace.onReady(function (){
//UI building
});
instead of directly using

ext.onReady(function (){
//UI building
});

this makes sure that language js will be loaded correctly across the application.
offcourse 'myNamespace.onReady ' definition will be in external common javascript include.
Is this the best approach.

rbastic
26 Apr 2010, 6:34 AM
No, it's not.

Like everyone else said, what you're doing is pointless.

extbio
26 Apr 2010, 6:55 AM
Overriding Ext.onReady is sometimes necessary, although such dynamically loading JS might not be the best programming practice. If you insist, I believe modifying your code like this would meet your needs:


myNamespace.onReady = function(fn, scope, options)
{
var language = //read language from cookie
Ext.Ajax.request({
url: "external_" + language + ".js",
success: function () {
eval(responseText);
Ext.onReady(fn, scope, options);
}
});
}
myNamespace.onReady(function (){
//UI building
});

alupuli
26 Apr 2010, 9:27 PM
extbio,rbastic,
thanks for your replies.

still wondering whats the best approach for below scenario.

A multi-lingual application.
where language information which is specific to each user is stored in a cookie on login itself.
Based on cookie value corresponding language js to be included.

but 'extbio' mentioned that its not best practice to load javascript files dynamically as explained in previous replies.

below code will not work as Ext.onReady fires before external language js is loaded.
language='english';//or other language values read from cookie


Ext.Ajax.request({
url: 'external_' + language + 'js',
success: function (response) {
eval(response.responseText);
alert(language_variable_in_external_english);
}
});
Ext.onReady(function () {
alert(language_variable_in_external_english);
})



So I tried over riding Ext.onReady and to use 'myNamespace.onReady' whose definition will be in a common external file.
By using ''myNamespace.onReady' reading cookie and loading external js for each UI can be avoided.

Rothariger
27 Apr 2010, 12:12 AM
Overriding Ext.onReady is sometimes necessary, although such dynamically loading JS might not be the best programming practice. If you insist, I believe modifying your code like this would meet your needs:


myNamespace.onReady = function(fn, scope, options)
{
var language = //read language from cookie
Ext.Ajax.request({
url: "external_" + language + ".js",
success: function () {
eval(responseText);
Ext.onReady(fn, scope, options);
}
});
}
myNamespace.onReady(function (){
//UI building
});


i didnt know i could do that, now one cuestion, if i do that and i load some panels and stuff... how can i ensure that all that data will be deleted for performance?

example:
with an .js i load a grid with a window to add something, now when im finished with that, i close the grid in some way and i want to release all that memory, is there a way to make this happend??
im not talking about the grid and window memory, im talking about the eval(.js) memory...

extbio
27 Apr 2010, 8:37 AM
@alupuli: Just design your own localization scheme like a JSON file: var allStrings = { "English":{"pass":"Enter Password"...}, "French":{"pass":"Mot de passe"...}. Include that JSON file in your html, and in your javascript, the old "alert("Enter Password")" would become "alert(allStrings.mylanguage.pass)". Then again, the override I showed you earlier should fit your need without doing any localization. It's easier if you already has "english.js" and "french.js" ready.

@Rothariger: If you dynamically included a JSON data file (like the localization file above) and would like to release memory after using it, just do allStrings = null; and garbage collector will reclaim the memory. If it's a JS file with many global objects, destroy those objects yourself through objects' destroy methods (if no destroy or similar method, just set it to null and leave it to garbage collection). But I doubt if it's worth the effort unless you're loading large JSON data file as I said.

Rothariger
27 Apr 2010, 10:42 AM
ok, thanks for your advice!!!