Login Register

Fetching Single Items and Values

For this example, we'll assume the following simple data source:

{ identifier: 'name', 
  items: [
	{ name: 'Adobo', aisle: 'Mexican' },
	{ name: 'Balsamic vinegar', aisle: 'Condiments' },
	{ name: 'Basil', aisle: 'Spices' },
	{ name: 'Bay leaf', aisle: 'Spices' },
        { name: 'Beef Bouillon Granules', aisle: 'Soup' },
// ...
	{ name: 'Vinegar', aisle: 'Condiments' },
	{ name: 'White cooking wine', aisle: 'Condiments' },
        { name: 'Worcestershire Sauce', aisle: 'Condiments' },
        { name: 'pepper', aisle: 'Spices' }
]}

Working with One Item

You might want to access items directly and work with one item at a time. Stores that implement the identity interface allow you to do this quite easily. In this example of accessing an item by its unique identifier, the following APIs are used:

Identity
fetchItemByIdentity() Fetches an item by its key value. Because the identity value of each item is unique, you are guaranteed at most one answer back.
Read
getValue() Takes an item and an attribute and returns the associated value

The following code fragment returns the aisle pepper is in:

var pantryStore = new dojo.data.ItemFileReadStore({
  url: "pantry_items.json" 
});


pantryStore.fetchItemByIdentity({
  identity: "pepper", 
  onItem: function(item){
    console.debug("Pepper is in aisle ", pantryStore.getValue(item,"aisle"));
  }
});

Note: In the previous example, the fetch is asynchronous. This is because many Datastores will need to go back to a server to actually look up the data and some I/O methods do not readily allow for a synchronous call.