PDA

View Full Version : Ext.application and Ext.override in launch function does not work?



arcan9ne
19 Oct 2011, 3:54 AM
I'm moving towards the ExtJS 4.0 MVC architecture and I had an override in my old init method which worked:

Old code that worked:


APP.app = function() {
...
return {
init: function() {
Ext.QuickTips.init();

Ext.override(Ext.Ajax.request, {
extraParams: {
param_1: Ext.get('my_id').getValue(),
param_2: Ext.get('my_id_2').getValue(),
param_3: Ext.get('my_id_3').getValue()
}
});
}
}
}

Ext.onReady(APP.app.init, APP.app);


New code that doesn't


Ext.application({
name: 'APP',
autoCreateViewport: true,
launch: function() {
Ext.override(Ext.Ajax.request, {
extraParams: {
param_1: Ext.get('my_id').getValue(),
param_2: Ext.get('my_id_2').getValue(),
param_3: Ext.get('my_id_3').getValue()
}
});
}
});


As you can see, I've moved this override to the launch function. You will also realize that I am retrieving my values from elements in the page after it has been loaded which is the reason for applying the override in the launch function.

Any help would be much appreciated.

arcan9ne
20 Oct 2011, 6:30 AM
My other thought would be to add a listener for when the page is loaded, but I don't see any events specific Ext.app.application. Are there any?

remeis
20 Oct 2011, 7:13 AM
Hi,

basically, I've the same problem: I'd like to override Ext.data.proxy.Server such that it will react on a failed request:

Ext.data.proxy.Server.override({
constructor: function() {
console.log('overriden data.proxy.Server constructed');
return this.callOverridden(arguments);
},

afterRequest: function(request, success) {
if (! request.operation.success) {
console.log('request failed');
}
}
});
This works if I put this code directly above the store definition in the corresponding store file (app/store/myStore.js). Because I'd like to apply this to every store (in app/store) it would make sense to override the class in the main application file (app.js). But above the application definition doesn't work:

<-put the code above here does not work->

Ext.application({
name: 'myApp',
appFolder: 'app',
...
)};
I will continue working on this problem.

remeis
20 Oct 2011, 11:50 AM
I got it working: I've moved the above code with the Ext.override directive to the launch function of the application, like you did:

Ext.application({
...
launch: function() {
Ext.data.proxy.Server.override({
...
});
...
}
});
First, it did not work either. Then I switched the ExtJS library from ext.js to ext-debug.js in the HTML header:

<script src="extjs/ext-debug.js" type="text/javascript"></script>
And it magically worked!

So I guess, in order to use ext.js again (for the final apllication version), there is something missing, maybe one has to require a specific class, which I'm not aware of?

arcan9ne
21 Oct 2011, 12:30 AM
Thanks a lot remeis, that worked for me as well! I will do some further testing and if I figure out what is missing then will post the solution using ext-all.js.

skirtle
21 Oct 2011, 12:38 AM
I think the most widely used technique for applying overrides in ExtJS 4 is:


Ext.require('classname', function() {
// Callback for when the class has been loaded. Apply the overrides here.
});

This would be placed before the call to Ext.application().

I'm a bit confused by the use of Ext.override() to set extraParams on Ext.Ajax.request. I don't really understand how that ever worked. As far as I can tell it'll be setting the extraParams on the prototype of the request function. Unless you're doing something really odd like new Ext.Ajax.request({...}) I can't see why they'd get picked up. Wouldn't this be a little more straightforward?


Ext.apply(Ext.Ajax.extraParams, {
param_1: Ext.get('my_id').getValue(),
param_2: Ext.get('my_id_2').getValue(),
param_3: Ext.get('my_id_3').getValue()
});

I also wonder whether you'd be better off using a cookie instead.

remeis
21 Oct 2011, 1:05 AM
I think the most widely used technique for applying overrides in ExtJS 4 is:


Ext.require('classname', function() {
// Callback for when the class has been loaded. Apply the overrides here.
});

Thanks skirtle! This code looks much more elegant and also works for me using ext.js.

arcan9ne
21 Oct 2011, 3:41 AM
Skirtle,

I am doing quite a few Ext.Ajax.requests, which all need these params. I thought it was simpler to set these params as an override.

As for the Ext.require technique, because the page has not loaded, the elements will not be found.

I've attempted the Ext.apply(Ext.Ajax.extraParams in the launch function but to no avail. I'll keep cracking at it as I don't like having to call the ext-debug.js file.

I'll keep cracking at it, but it seems that my best bet might be to go back to init method.

arcan9ne
21 Oct 2011, 3:44 AM
Skirtle,

You also mentioned that it was odd to use Ext.Ajax.requests, is there an alternative I'm not aware of? :-?


Regards,
arcan9ne

skirtle
21 Oct 2011, 10:20 AM
You also mentioned that it was odd to use Ext.Ajax.requests, is there an alternative I'm not aware of?

No, that's fine. What I meant was that it would be odd to invoke it using the new operator. I only mentioned this because it was the only way I could think of that your code could possibly have worked.

As for the elements in the page. What are they, hidden input fields? Depending on your choice of server-side technology you might find it simpler to inject those values directly as JavaScript variables rather than going via hidden inputs. You should take care to consider escaping the values appropriately if you do go down this route.

From a timing perspective, another possible alternative might be to do this using the Ext.Ajax.beforerequest event. This would ensure that you are making changes as late as possible before the first Ajax request takes place.