PDA

View Full Version : How best to implement similar configuration of proxy across various models and stores



gnube
23 Nov 2011, 9:18 AM
I have some models I have created for MVC framework with almost the exact same proxy setup - basically the model name and the url on my rest proxy changes - is there a nice way to share the proxy definition in a class I can extend (or similar)?

Instead of having a bunch of classes all with the same detail - like this:


Ext.define('MyApp.model.modelT', {
extend: 'Ext.data.Model',
defaultProxyType: 'rest',
fields: [ … ],

proxy: {
type: 'rest',
url: 'http://hostname/restful.php/modelT',

reader: {
type: 'json',
root: 'data'
},

writer: {
type: 'json'
}
}
});



What would be a nice way to reuse/share the proxy detail and replace the url for individual model definitions?

tobiu
23 Nov 2011, 9:26 AM
create an extension of the proxy with your custom configs or write a factory method for it.

gnube
23 Nov 2011, 9:36 AM
Hi tobiu,

The bit I am having trouble with is seeing how to implement the concept - If I create a proxy class, how do I incorporate that into the "proxy:" part of the model JSON?

I am sure it's obvious once I see it but I can't see it.

Do you have time to pencil in for me?

Thx

tobiu
23 Nov 2011, 9:40 AM
take a look at:
http://docs.sencha.com/ext-js/4-0/source/Rest.html#Ext-data-proxy-Rest



Ext.define('Ext.data.proxy.Rest', {
extend: 'Ext.data.proxy.Ajax',
alternateClassName: 'Ext.data.RestProxy',
alias : 'proxy.rest',


use an own alias and reference it that way.

gnube
23 Nov 2011, 10:54 AM
The bit that was eluding me was how to incorporate they new proxy class into my model. I figured it was going to be one of those 'doh!' moments. I simply needed to use Ext.create



Ext.define('MyApp.model.requestors', {
extend: 'Ext.data.Model',

fields: [ ....
],

proxy: Ext.create( 'MyApp.data.proxy.MyRestProxy',{
url: 'http://hostname/restful.php/requestor'
})
});


Where MyApp.data.proxy.MyRestProxy is an extension of Ext.data.proxy.Rest with my reader, writer, listeners etc added.

thx for your input tobiu

tobiu
23 Nov 2011, 11:20 AM
well, you don't even need that :)



Ext.define('MyApp.model.modelT', {
extend: 'Ext.data.Model',
fields: [ … ],

proxy: {
type: 'myproxytype'
}

gnube
23 Nov 2011, 12:08 PM
I like it!