Using with a DojoX DataGrid

By on July 15, 2011 4:32 pm

As a part of our Free Dojo Support initiative, we received the following request for a CRUD example using with a Dojo grid

The Question

“Dojo 1.6 supposedly has a new data access scheme, but I’m finding it unusable because I can’t find any complete working examples for something as basic as Create, Read, Update, Delete when using a Dojo grid ultimately populated from a store. I’ve created my JsonRest servlet, but the Dojo data grids only seem to have the necessary behavior for handling the R part of CRUD. I cannot find a complete CRUD working example of using with a dojo grid.

Our Response

As of Dojo 1.6, there is a new data store API, called Dojo Object Store. There is currently an introductory tutorial on Dojo Object Stores, but you’re right that it doesn’t cover the exact details of CRUD.

The general idea behind the Dojo Object Store API is that it has a simple core API. There are certain methods that are defined for stores that implement them. If the method doesn’t exist, it means the store doesn’t implement it. For example, you could have a read-only store simply by implementing just the get and query methods. If we look at the JsonRest store, it implements the following methods:

  • add: The Create method, used to take a new object and store it
  • get: The Read method, used to get an object by identity
  • put: The Update method, used to take an object that you have and push it back into the store with whatever modifications you may have made
  • remove: The Delete method, which will remove an object when given an identifier

These aren’t the only methods that it implements, but these are the core of CRUD. The JsonRest store is instantiated with a URL, and it’ll map these API calls into the appropriate GET/PUT/POST/DELETE calls for you.

This is all well and good, but the DataGrid expects to receive a store. Fortunately, there is a wrapper in dojo/data/ObjectStore that you can hand a new Dojo Object Store (such as the JsonRest store) into, and it returns a Dojo Data Store that implements the Dojo Data API, allowing you to bind your new store to your DataGrid. The wrapper will expose the appropriate Read/Write/Identify Dojo Data API endpoints as appropriate based on the store handed to it, ensuring that any data-aware widgets (such as the DataGrid) will be able to properly handle all of the CRUD aspects for you.

We’re releasing a new tutorial, Connecting a Store to a Grid, to demonstrate this technique, including a full example. Have a look and try out the code yourself!

Next Request Please!

Have a Dojo question you’re just dying to ask? Get your free Dojo support now! Or sign-up for the Best JavaScript and Dojo Support available to get all of your questions answered all the time!