Emilio
Emilio

Reputation: 1044

ItemFileReadStore fetch doesn't works if response data has nested objects

I'm using Dojo toolkit to get data from an external JSON source (a web service) via ajax request. I'm using this retrieved data to create an ItemFileReadStore, and then I use its fetch method to print some attributes of the objects that it contains.

JSON source has nested attributes, an example here:

[    
 {
  "name":"michael",
  "surname":"owen",
  "reference": [
    {
      "code":21,
      "date":"01-01-2001"
    },
    {
      "code":134,
      "date":"11-05-2011"
    }
  ]
 },
 {
  "name":"robert",
  "surname":"foreman",
  "reference": [
    {
      "code":33,
      "date":"11-10-2009"
    }
  ]
 },
]

As you can see, "reference" is an array of objects with two attributes each one.

The problem: I create an ItemFileReadStore with this data and when I try to fetch it doesn't shows anything. I've been testing this and I confirm that it works with plain estructures (without nested objects).

I think probably the store is not well-created.

Code of the fetch:

var store = ItemFileReadStore({data: response});  
store.fetch( { query: {'name': 'michael'}, onItem: function(item) {
                            console.log(store.getValue( item, 'surname' ));
                        }
                    });

So, the question is: Could I read nested structures somehow? maybe with another kind of store? Using another syntax?

Thanks!

Upvotes: 0

Views: 597

Answers (1)

Craig Swing
Craig Swing

Reputation: 8162

The answer depends on how you want to use the store.

The new dojo.store API allows you to pass a function as the query and you can do the custom filtering in the function.

http://dojotoolkit.org/features/1.6/object-store

However, many of the current dojo widgets still use the dojo.data API (ItemFileReadStore). There is an adapter, but you cannot pass a function into the query.

Some widgets like the grid, allow you to set the items directly and not have to query. If that is the case, you can use the new store API to query and manually set the items onto the grid.

There's also dojox.json.query, but I have never used it.

Upvotes: 1

Related Questions