PDA

View Full Version : Create reader dynamically?



sb32
15 Dec 2007, 11:55 AM
Guys, please help. Just trying to figure out how I can create the reader hash dynamically, instead of hard coding {name: 'milk', mapping: 'milk'}

Ideally, it would be a part of the Json response. Currently, the Json is:

{"prices"=>{:milk=>"3.02", :bread=>"2.67"}}

If on the server side, I were to extend this to:

{"products" => ["milk", "bread"], "prices"=>{:milk=>"3.02", :bread=>"2.67"}}

How could I make the client side js build the readre based on the products array?

Thanks so much


<script type="text/javascript">

function getFieldValue(store, rowIndex, fieldName) {
var myDataRecord = store.getAt(rowIndex);
return myDataRecord.get(fieldName);
}
function display() {
var ds = new Ext.data.Store({
proxy: new Ext.data.ScriptTagProxy({url: 'http://somewebsite.com/prices'}),
reader: new Ext.data.JsonReader({
root: 'prices'
},
[
{name: 'milk', mapping: 'milk'}
])
});
ds.load();
document.getElementById('milk').innerHTML = getFieldValue(ds, 0, 'milk');
}
</script>
<body onLoad="display()">
<div id="milk"></div>
</body>
</html>

sb32
15 Dec 2007, 10:11 PM
I'm all ears as to what's nasty about this but.. hey, I figured out a way to make it work

Hope this helps the next person:
json:

(({products: [{product: "milk"}, {product: "bread"}], prices: [{milk: "$3.45", bread: "$2.83"}]}));

js:


function display() {
products = new Array();
proxy = new Ext.data.ScriptTagProxy({url: 'http://somesite.com/cheap/prices'});
productsReader = new Ext.data.JsonReader({root: 'products'}, [{name: 'product'}]);
dsProducts = new Ext.data.Store({proxy: proxy,reader: productsReader});
dsProducts.on('load', function(store, records, options){
count = dsProducts.getCount();
for(var i=0; i < count; i++ ){products.push({name: dsProducts.getAt(i).get('product')});}
pricesReader = new Ext.data.JsonReader({root: 'prices'},products);
dsPrices = new Ext.data.Store({
proxy: proxy,
reader: pricesReader
});
dsPrices.on('load', function(store, records, options){
for(var i=0; i < count; i++ ){
try {Ext.get('s1-' + products[i]['name']).update(dsPrices.getAt(0).get(products[i]['name']));}
catch(err){}
}
});
dsPrices.load();
});
dsProducts.load();
}

sb32
6 Jan 2008, 7:41 AM
I don't like this, because it makes two json requests to the server instead of one.

Any way to duplicate a data store without having it reconnect? I've looked at this quite a bit to no avail, please help

hendricd
9 Jan 2008, 3:35 AM
Store cloning. (http://extjs.com/forum/showthread.php?t=18559&highlight=store+clone)