PDA

View Full Version : Problem with hasMany-association and model-proxy



phill54
7 Jul 2015, 12:34 AM
Hi everyone,

I've run into a weird problem that you may checkout on jsfiddle:
https://jsfiddle.net/phill54/bewd46ka/21/

I'm failing at associating two models when the nested model uses a custom proxy.
I was hoping someone would be able to help me out why that is and if there's a workaround for that.

In the jsfiddle-example everything works fine. The problem occurs when trying to uncomment the proxy of the order.line model.


Ext.define('Order',{
extend: 'Ext.data.Model'
,fields: [ 'id', 'customerId', 'totalGross']
,associations: [{
type: 'hasMany',
model: 'Order.Line',
associationKey: 'lines',
name: 'lines',
primaryKey: 'id',
autoLoad: false,
foreignKey: 'orderId'
}]
,proxy: {
type: 'custom-rest',
url: '/api/order', // redirected to /echo/json/ when loading
extraParams: {
'with': 'lines'
}
}
});


Ext.define('Order.Line', {
extend: 'Ext.data.Model',
fields: ['id', 'title', 'totalGross']
/* if you uncomment the following 4 lines, the demo wont
* work no more. wtf?!? why is that? can anyone help?
*/
// ,proxy: {
// type: 'custom-rest',
// url: '/api/order/line'
// }
});

phill54
7 Jul 2015, 2:12 AM
I figured it's because of the proxy reader configuration.
https://jsfiddle.net/phill54/bewd46ka/22/

when i configure my proxy reader to point to specific success/message/total-Properties, the nested model causes the whole store.load() to fail.

has anyone an idea how to fix this?
do i need seperate proxy-readers for nested and not-nested models?


/* this is the proxy i want to use for all of my models */ Ext.define('CustomProxy', {
extend: 'Ext.data.proxy.Rest',
alias: 'proxy.custom-rest',
sortParam: 'order',
startParam: (function () {})(), //set to undefined
pageParam: 'page',
limitParam: 'pageSize',
actionMethods: {
create: 'POST', read: 'POST',
update: 'POST', destroy: 'POST'
},
reader: {
type: 'json'
,root: 'data'
/* if i uncomment the following 3 lines, the demo wont
* work no more. wtf?!? why is that? can anyone help?
*/
// ,successProperty: 'meta.success'
// ,messageProperty: 'meta.message'
// ,totalProperty: 'cursor.totalCount'
}
});

phill54
7 Jul 2015, 2:37 AM
solved.

https://jsfiddle.net/phill54/bewd46ka/23/


Ext.define('Order',{
extend: 'Ext.data.Model'
,fields: [ 'id', 'customerId', 'totalGross']
,associations: [{
type: 'hasMany',
model: 'Order.Line',
associationKey: 'lines',
name: 'lines',
primaryKey: 'id',
autoLoad: false,
foreignKey: 'orderId',
reader: {} // override default reader of model !!!
}]
,proxy: {
type: 'custom-rest',
url: '/api/order', // redirected to /echo/json/ when loading
extraParams: {
'with': 'lines'
}
}
});

I had to override the default reader of my custom-proxy with an empty reader when associating the nested model.