foxwhisper
5 Dec 2008, 8:48 AM
Hey guys,
I ran into an issue today where I wanted to apply a data object from a Store, onto a XTemplate loop. An example of this was:
<h1 class="model_sec_header">Users</h1>
<div class="model_sec_body">
<tpl for="users">
<div class="model_sec_row">
<p style="font-weight:bold; float:left; width:100px; ">{username}</p>
<p style="float:left; width:150px; ">{password}</p>
<p style="float:left; width:170px; ">{email:ellipsis(25)}</p>
</div>
</tpl>
</div>
I then wanted to apply the data store against a template, like this:
new Ext.XTemplate(document.getElementById('addNewCustomer_card6').value).apply(dataStoreHere)
However, the data store did not have any public method or property which outputted the data in a format which XTemplate approved of. So, I wrote the following override to compensate:
Ext.override( Ext.data.Store, {
convertToObject: function() {
var obj = []
for (x=0;x<this.getCount();x++) {
obj.push(this.getAt(x).data)
}
return obj
}
});
Then, to apply the data struct against the template, just use:
struct = {
users:Ext.getCmp('usergrid').store.convertToObject()
}
alert(new Ext.XTemplate(document.getElementById('textAreaElementContainingHTMLCode').value).apply(struct))
That then gives you a template fully formatted with the data struct.
Enjoy!
I ran into an issue today where I wanted to apply a data object from a Store, onto a XTemplate loop. An example of this was:
<h1 class="model_sec_header">Users</h1>
<div class="model_sec_body">
<tpl for="users">
<div class="model_sec_row">
<p style="font-weight:bold; float:left; width:100px; ">{username}</p>
<p style="float:left; width:150px; ">{password}</p>
<p style="float:left; width:170px; ">{email:ellipsis(25)}</p>
</div>
</tpl>
</div>
I then wanted to apply the data store against a template, like this:
new Ext.XTemplate(document.getElementById('addNewCustomer_card6').value).apply(dataStoreHere)
However, the data store did not have any public method or property which outputted the data in a format which XTemplate approved of. So, I wrote the following override to compensate:
Ext.override( Ext.data.Store, {
convertToObject: function() {
var obj = []
for (x=0;x<this.getCount();x++) {
obj.push(this.getAt(x).data)
}
return obj
}
});
Then, to apply the data struct against the template, just use:
struct = {
users:Ext.getCmp('usergrid').store.convertToObject()
}
alert(new Ext.XTemplate(document.getElementById('textAreaElementContainingHTMLCode').value).apply(struct))
That then gives you a template fully formatted with the data struct.
Enjoy!