PDA

View Full Version : How to extend a library model and pass parameters to the preconfigured proxy?



cmeans
2 Nov 2012, 4:30 PM
ExtJS v4.1.1a

I have a library model that includes a mostly preconfigured proxy.

How do I extend that model and properly pass parameters to the proxy?

Here's the library model:


Ext.define(
'My.Model',
{
extend: 'Ext.data.Model',

fields:
[
'foo', 'bar'
],

proxy:
Ext.create(
'My.Proxy',
{
extraParams:
{
output: 'json'
}
})
});


I can't seem to get a constructor working on the above library class.

I'd like to be able to pass in more properties for the proxy's extraParams object.

Thanks.

vietits
2 Nov 2012, 4:59 PM
Try this:


Ext.define('My.SubModel', {
extend: 'My.Model',
constructor: function(){
this.callParent(arguments);
Ext.apply(this.proxy.extraParams, {
param1: 'Value 1',
param2: 'Value 2'
});
}
});

cmeans
2 Nov 2012, 6:13 PM
Does not seem to work.

vietits
2 Nov 2012, 6:27 PM
The following code works fine for me with Ext 4.1.1 and Chrome 22:


Ext.onReady(function(){
Ext.define('My.Proxy', {
extend: 'Ext.data.proxy.Ajax',
alias: 'proxy.myproxy',
url: 'index.php',
reader: {
type: 'json'
}
});


Ext.define('My.Model', {
extend: 'Ext.data.Model',
fields: [
'foo', 'bar'
],
proxy: Ext.create('My.Proxy', {
extraParams: {
output: 'json'
}
})
});
Ext.define('My.SubModel', {
extend: 'My.Model',
constructor: function(){
this.callParent(arguments);
Ext.apply(this.proxy.extraParams, {
param1: 'Value 1',
param2: 'Value 2'
});
}
});

var model = Ext.create('My.SubModel');
console.log(model.proxy.extraParams);
});

However, I suggest you not define My.Model this way because it will share proxy object on its prototype and this will cause problem if you use many instances of My.Model or its sub classes.

cmeans
3 Nov 2012, 6:00 AM
OK.

So you're suggesting it's just better to create the proxy in the final model implementation?

I've been trying to package up library code to ease development (meaning we can just decend from one class and have everything setup), but I guess this isn't that much extra work.

Thanks.

vietits
3 Nov 2012, 4:33 PM
No, my suggestion is not sharing proxy object on your My.Model prototype.


Ext.define('My.Model', {
extend: 'Ext.data.Model',
fields: [
'foo', 'bar'
],
// proxy: Ext.create('My.Proxy', { <- this will share proxy object on prototype
// extraParams: {
// output: 'json'
// }
// }),
constructor: function(config){
config = config || {};

// get extra params from config
var extraParams = Ext.apply({
output: 'json' // default extra params
}, config.extraParams);
delete config.extraParams;

// create a proxy object for each My.Model instance
this.proxy = Ext.create('My.Proxy', {
extraParams: extraParams
});
this.callParent([config]);
}
});


Ext.define('My.SubModel', {
extend: 'My.Model',
constructor: function(config){
config = config || {};
config.extraParams = Ext.apply(config.extraParams || {}, {
param1: 'Value 1',
param2: 'Value 2'
})
this.callParent([config]);
}
});

var model = Ext.create('My.SubModel', {extraParams: {param3: 'Value 3'}});
console.log(model.proxy.extraParams);

cmeans
3 Nov 2012, 7:19 PM
Unfortunately, the constructor is not being called on My.Model.

Not sure why.

I'm using ExtJS v4.1.1a.

cmeans
3 Nov 2012, 7:30 PM
I've moved the proxy construction to the store. This actually makes more sense.

I can have 1 model, and multiple proxies, and I decide for each store which proxy to use with a given model.

Seems right.