Berzzzebub
27 Aug 2011, 3:26 AM
Hello community.
I'm trying to use data from association in convert function but encountered with very strange behavior of store with association data that I can not explain.
I have two models:
Ext.define('my.model.LabeledAddress', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'value', type: 'string'},
{name: 'contact_id', type: 'string'}
]
});
and
Ext.define('my.model.Contact', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'caption', type: 'string'},
{name: 'picture', type: 'string'},
{name: 'state', type: 'string', defaultValue: 'offline'},
{
name: 'labAddrs',
type: 'string',
convert: function(value, record) {
var addresses = record.labeledAddresses();
var val = '';
console.log(addresses); //Normal Store object with associated items
console.log(addresses.count()); //Returns 0
addresses.each(function(rec) {
val.concat(rec.get('value'), ' '); //Never called
});
return val;
}
}
],
associations: [
{type: 'hasMany', model: 'my.model.LabeledAddress', name: 'labeledAddresses', foreignKey: 'contact_id'}
],
proxy: {
type: 'ajax',
api: {
read: '/my_client/get_contacts'
}
}
});
In second one (Contact) I want to create field that will return concatenation of associated addresses values. As I mentioned in comment in code, record.labeledAddresses() returns normal store object, that contains all needed data, but when I'm trying to fetch it, the store behaves like absolutely empty.
An example of JSON, that returns from /my_client/get_contacts:
{
"id":"4dd0e431e73b361ad2000098",
"caption":"Administrator",
"picture":"images/DefaultAvatar.png",
"labeledAddresses":
[
{"id":"4e55fea1e73b3633d700014a","contact_id":"4dd0e431e73b361ad2000098","value":"202"},
{"id":"4e55fea1e73b3633d700014c","contact_id":"4dd0e431e73b361ad2000098","value":"204"}
],
"directory_id":"4dd0e431e73b361ad2000097"
}
What am I doing wrong? I would be grateful for any advice.
I'm trying to use data from association in convert function but encountered with very strange behavior of store with association data that I can not explain.
I have two models:
Ext.define('my.model.LabeledAddress', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'value', type: 'string'},
{name: 'contact_id', type: 'string'}
]
});
and
Ext.define('my.model.Contact', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'caption', type: 'string'},
{name: 'picture', type: 'string'},
{name: 'state', type: 'string', defaultValue: 'offline'},
{
name: 'labAddrs',
type: 'string',
convert: function(value, record) {
var addresses = record.labeledAddresses();
var val = '';
console.log(addresses); //Normal Store object with associated items
console.log(addresses.count()); //Returns 0
addresses.each(function(rec) {
val.concat(rec.get('value'), ' '); //Never called
});
return val;
}
}
],
associations: [
{type: 'hasMany', model: 'my.model.LabeledAddress', name: 'labeledAddresses', foreignKey: 'contact_id'}
],
proxy: {
type: 'ajax',
api: {
read: '/my_client/get_contacts'
}
}
});
In second one (Contact) I want to create field that will return concatenation of associated addresses values. As I mentioned in comment in code, record.labeledAddresses() returns normal store object, that contains all needed data, but when I'm trying to fetch it, the store behaves like absolutely empty.
An example of JSON, that returns from /my_client/get_contacts:
{
"id":"4dd0e431e73b361ad2000098",
"caption":"Administrator",
"picture":"images/DefaultAvatar.png",
"labeledAddresses":
[
{"id":"4e55fea1e73b3633d700014a","contact_id":"4dd0e431e73b361ad2000098","value":"202"},
{"id":"4e55fea1e73b3633d700014c","contact_id":"4dd0e431e73b361ad2000098","value":"204"}
],
"directory_id":"4dd0e431e73b361ad2000097"
}
What am I doing wrong? I would be grateful for any advice.