PDA

View Full Version : Get specific index of an object?



darren
23 Feb 2010, 3:11 PM
Hi guys I am using the following code to get information:



products = {
count: 0,
loadJson: function() {
var count = 0;
var productsStore = new Ext.data.JsonStore({
fields: [
{ name: 'image', mapping: 'image' },
{ name: 'name', mapping: 'name' },
{ name: 'description', mapping: 'description' },
{ name: 'price', mapping: 'price' }
],
url: '/products/json',
listeners: {
load: function(objects) {
objects.each(function(product, index) {
if (index == 0) {
Ext.get('product-image').dom.src = '/uploads/products/'+product.get('image');
}
count++;
});
products.count = count;
}
}
});
productsStore.load();
}
}


However I would prefer I did not have to do .each to just get data from a specific index of an object. I have tried object[0] but I get an error every time. Is there an alternative?

evanc
23 Feb 2010, 3:45 PM
The first argument passed in to "load" is the store itself. You can just do store.getAt(0).get('image').

Are you really using a whole jsonStore just to get a single element of a JSON string? Do you know about Ext.decode()?

darren
23 Feb 2010, 11:13 PM
Hi thanks yes I figured those things out shortly after. I am not using a whole jsonStore for one string, just getting the first result as a main display for what im building. I load all so they can be later accessed.