-
22 Dec 2010 1:08 AM #1
How to render a hasmany association with a template?
How to render a hasmany association with a template?
Let's say I have a simple hasMany relation such as the one in the docs (using localstorage):
When I load a User model, user.data.products is an empty array. I can access the related data (products in the previous example) through the user.products(), after calling user.products().load(). However, user.data.products keeps being an empty array. Thus, creating a simple template such as:Code:Ext.regModel('Product', { fields: [ {name: 'id', type: 'int'}, {name: 'user_id', type: 'int'}, {name: 'name', type: 'string'} ], proxy: { type: 'localstorage', id: 'myapp-Products' } }); Ext.regModel('User', { fields: [ {name: 'id', type: 'int'}, {name: 'name', type: 'string'} ], hasMany: {model: 'Product', name: 'products'}, proxy: { type: 'localstorage', id: 'myapp-Users' } });
renders no products when called with the user object.Code:myTemplate = new Ext.XTemplate( '<tpl for=".">' + ' <h2>{name}</h2>' + ' Products:' + ' <ul>' + ' <tpl for="products"><li>{name}</span></li></tpl>' + ' </ul>' + '</tpl>');
Of course, when I call user.products().load() I could manually insert the elements in the .products() store into the user.data.products array, but it sounds like I big ugly hack. Is there a proper/better way to render related data with a template? (or to load it into the model object).
Thanks,
Salva.
-
18 Apr 2011 7:00 AM #2
I also need help with this.
Salva, did you have any luck with this?
-
23 Aug 2011 4:35 AM #3
Hmmpf has nobody an idea ? I have the same problem ...
-
14 Dec 2011 7:29 AM #4
I'm guessing this isn't supported?
I'm guessing this isn't supported?
The silence can only suggest that this can't be done.
-
22 Dec 2011 12:04 PM #5
I'm with difficulties in this too
I'm with difficulties in this too
I'm trying to update an panel with template generated of a model:
I try various combinations of record.enderecos(), record.enderecos.dataHTML Code:mostrarDetalhes: function(grid, record){ var tpl = new Ext.XTemplate( '<tpl for=".">', '<p>{rua}</p>', '</tpl>' ); var endereco = tpl.apply(record.enderecos().data.items); console.log(endereco); console.log(record.enderecos()); this.getDetalhes().update(endereco); }
If XTemplate expects an array there is no way to pass a model, because model use Objects and arrays of objects to represent the data.
I will try later iterate over the record object and getting an array and pass to XTemplate or creating an result direct.
-
22 Dec 2011 2:03 PM #6
It Works
It Works
How I made this works:
mostrarDetalhes: function(grid, record){
var tpl = new Ext.XTemplate(
'<tpl for=".">',
'<div class="endereco">',
'<p><span class="titulo">Endereço - {#}</span></p>',
'<p>Rua: {rua}, {numero}</p>',
'<p>Bairro: {bairro}</p>',
'<p>Cidade: {cidade} - {estado}</p>',
'<p>CEP: {cep}</p>',
'<p>Complemento: {complemento}</p>',
'</div>',
'</tpl>'
);
var enderecoTplResult = "";
record.enderecos().each(function(end){
enderecoTplResult = enderecoTplResult.concat(tpl.apply(end.data));
console.log(enderecoTplResult);
});
this.getDetalhes().update(enderecoTplResult);
}
Iterate over the association and get the data property
-
22 Dec 2011 3:04 PM #7
Or better:
mostrarDetalhes: function(grid, record){
var tpl = new Ext.XTemplate(
'<tpl for=".">',
'<div class="endereco">',
'<p><span class="titulo">Endereço - {#}</span></p>',
'<p>Rua: {rua}, {numero}</p>',
'<p>Bairro: {bairro}</p>',
'<p>Cidade: {cidade} - {estado}</p>',
'<p>CEP: {cep}</p>',
'<p>Complemento: {bairro}</p>',
'</div>',
'</tpl>'
);
var enderecoTplResult = "";
var data = [];
record.enderecos().each(function(end){
data.push(end.data);
});
enderecoTplResult = tpl.apply(data);
this.getDetalhes().update(enderecoTplResult);
}
The difference is the construction of data array for pass to template. It allows the count of iteration using the {#}
-
6 Jan 2012 7:30 AM #8
This is how I do it as well with Ext JS 4.x and this leaves a lot to be desired.
It completely breaks the encapsulation and API provided by Ext.data.Model and requires you to dwell in the bowels of the class implementation.
I wish XTemplate would be extended to work more nicely with associations.aka Seboss
-
20 Feb 2013 5:43 AM #9
As of 4.1, the getData(True) of a model record will return associated data that can be used with a template - it's just a question of calling it.
Similar Threads
-
[FIXED] Can't have more than 1 hasMany association
By stormin_walker in forum Sencha Touch 1.x: BugsReplies: 19Last Post: 19 Jan 2012, 2:05 PM -
[DataView] cant render the template
By localhost in forum Ext 2.x: Help & DiscussionReplies: 1Last Post: 12 Oct 2008, 2:23 PM -
[2.2] GridView: body template ignored on first render
By vivid-planet in forum Ext 2.x: BugsReplies: 0Last Post: 5 Aug 2008, 11:35 PM -
When does the template render?
By shenku in forum Ext 2.x: Help & DiscussionReplies: 0Last Post: 12 Nov 2007, 10:54 PM -
ext.template: possible to use a render-function outside the template-object?
By tobiu in forum Ext 1.x: Help & DiscussionReplies: 4Last Post: 20 Aug 2007, 9:18 AM


Reply With Quote