Login Register

best practices for dojo.data references?

I am trying to understand how to use the dojo.data api. Before this api, I had hand-coded a "dao" type pattern, with each dao loading json into essentially an array, and proving accessors. The one problem that I don't see how to solve is references from one datastore into another. for example, I have a list people in a people data store. each person has a category. i have a seperate category data store, and the category id is all that is listed in the json of the list of people. so how do i make that automagically a reference to the item in the category data store? is there a way to use typeMap to do this? I could imagine sending the typemap something that explains this, and looks up the id in the category data store, but that could not be asynchronous, right? help!

thanks in advance,

maulin

stores don't "do" references...

So one of the things we were pretty explicit about when designing stores was to ensure that they didn't "look" relational. The goal here is to avoid any of the patterns that might cause stores to need lots of data locally in order to satisfy references and as a result, dojo.data stores map better onto representing the results of services than they do in handling inter-item relationships. While frustrating for you I'm sure, this is entirely by design. if you need this functionality, you'll have to build it yourself at this point.

--
Project Lead, The Dojo Toolkit
President, The Dojo Foundation

You could colocate your data if you want an implicit reference

If we have data that is related in some way, we put it all into the same store and use references.
http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-...

You can do separate fetches for people and category.
If people -> category is one-to-many or many-to-one, then just make the 'one' part the child and the 'many' part the parent, e.g. if a category can have many people, but a person can only belong to 1 category, then make category the parent and add the people as children to the category.
Different issues arise if you have complex relationships in your data, e.g. people <-> category is many-to-many.

Having just read the above reply, I would concur that you should not hack your data to fit into a store and you could always hand-roll your own associative arrays using the stuff in dojox.collections. Only if your data is related in a simple hierarchy would I advocate the approach I outline.

thank you!

Thanks SO MUCH Alex for the response. I do tend to get bogged down in trying to use existing code to solve my problems. Now that I see that this is not really the intent of dojo.data, then it makes it much simpler. I have essentially very few categories, and didn't want to bog down my json with repeating category info in each record, er.. item. So its a simple enough problem to solve -- 1. suck it up and repeat category info, or 2. load categories via an explicit and sync'd xhr call, and keep people in dojo.data. then perform the lookup when i need to. (i.e. TeamsDao.getById(store.getValue(item, "categoryId"))).