PDA

View Full Version : Specify model property as collection of another model type?



basememara
5 Feb 2012, 10:34 AM
In my model, I am trying to make one of my properties a collection of a certain model, but Ext JS is not recognizing it as being the model. The reason I think this is because the custom properties from the child objects are not showing. It has the values from the proxy JSON request, but not the Ext JS custom property.

Here is my parent model:


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

fields: [
'FirstName',
{ name: 'Orders', type: 'Order' } //A COLLECTION OF ORDERS
]
});

This is my child model:


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

fields: [
'ID',
'Description',
//CUSTOM PROPERTIES
{
name: 'ShortDescription',
convert: function(v, record) {
return Ext.util.Format.ellipsis(record.data.Description, 50);
}
},
]
});

Then in my template, I am trying to get the nested order object but nothing displays for {ShortDescription}, I can only render out real properties and not the constructed ones:

[tpl: = [
'<header><h1>{FirstName}</h1></header>',
'<ul>',
'<tpl for="Orders">',
'<li><a href="{ID}" tooltip="{Description}">{ShortDescription}</a></li>',
'</tpl>',
'</ul>'
]
For the above template, {ID} and {Description} gets rendered out, but {ShortDescription} is blank. I have orders on a different template where it is a first level or root object and {ShortDescription} gets rendered fine in that scenario. It is just when it is a child property of another object.

Anybody know what I am missing? I have also tried to use hasMany association, but didn't work. I think because my stores are memory readers, so I was hoping to handle this on the model level and not the store.

mitchellsimoens
5 Feb 2012, 11:28 AM
The type cannot be a model. The available types are here: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Field-cfg-type

dedoz
5 Feb 2012, 12:39 PM
If your model is trying to represent "User has many orders" check documentation for model associations (you can look at the models)


so you do something like


Ext.define('MyApp.model.User', {
extend: 'Ext.data.Model',
fields: ['FirstName'],
hasMany : {model: 'Order', name: 'orders'}
});

just check the associations documentation http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Association
(http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Association)

basememara
5 Feb 2012, 2:43 PM
I have tried using hasMany, but I am still missing my custom properties. So I think that association has more to do with the store and not the model.

dedoz
6 Feb 2012, 1:11 AM
missing in which way ? explain
i just developed a code where
i have a model and associations,
edit that model and associations in 1 form.
show that model and its associated models data in 1 template.

basememara
6 Feb 2012, 6:06 AM
In my child model, I have a custom property called "ShortDescription":



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

fields: [
'ID',
'Description',
{
name: 'ShortDescription',
convert: function(v, record) {
return Ext.util.Format.ellipsis(record.data.Description, 50);
}
},
]
});

When I load my Orders store to a dataview for example, I can add {ShortDescription} onto the tpl and it works fine. With hasMany in my parent model (User), I tried loading the Users store into a dataview and put a <tpl for="Orders"..> in the tpl. All the order properties get rendered out except my custom property {ShortDescription}. It is almost as if the app does not know it is an Order model and just rendering out the data. Or maybe this is an overlook in the framework?

dedoz
6 Feb 2012, 7:08 AM
you are right, associations exists but they have no support in the framework, weird.
you still can work with associations in templates, is just templates wont work with association directly you need to "help them" :D
Can you post the code for the component that shows your user/orders ? maybe i can help you (maybe not ;D im new to extjs too)