REST with dojo and PHP: Notes on using dgrid with a caching store

I would like to share some (personal) pitfalls I came across when creating the demo REST with dojo and PHP: Demo of a dgrid observing changes to the JsonRest store:

  • dojo/store/Observable only works correctly when the object has an id. Unfortunately, dojo/store/Memory.add(object) does not add the id to the object when creating it (as of 1.7.1) .  See bugs #12835 and #14281. For a fix see changeset #27072
  • Observable does only work correctly with a store, that provides a queryEngine. dojo/store/JsonRest does not have a queryEngine by default, so you will have to provide your own. If you are using a cache, observe the dojo/store/Memory directly. Upate 29.06.2012: For a fix see changeset #29069.
  • As stated in the docs, but easily missed: dojo/store/Cache automatically adds all returned objects from get() and query() calls to the cache, but only get() uses the cache, whereas query() directly uses the master store. If you want the query to come from the cache, directly call query() on the caching store, e.g. dojo/store/Memory.
  • In the demo, the PHP script responds to the GET 2 by returning a Json object with id 2 and to a GET 2/ by returning an array of objects with parent id 2. If you want to query the Memory store for 2’s children, you have to provide an object as an argument, e.g. storeMemory.query({parId: 2}), whereas the JsonRest store expects a string e.g. storeJsonRest.query(‘2/’);
  • JsonRest store’s methods do not return the same types as Memory store, which can be confusing…
    storeMemory.add(newObject) returns the id
    storeJsonRest.add(newObject) returns the response from the server (or rather a Deferred providing it)
    …, but makes sense, because it can save an extra round trip to the server.

Also see dojo/store/Observable and dojo/store/Cache more info.

 

Join the Conversation

18 Comments

Leave a Reply to Simon Cancel reply

Your email address will not be published. Required fields are marked *

  1. Hi Simon,

    Bumped into your site/example when looking for dgrid usage… The remoteFileExplorer you mentioned above is not loading.. is it working?
    Also, can you tell if your dgrid implementation can filter on the columns.. can you direct me where/how it can be done?

    Thanks,
    Rich

    1. Sorry, you’re right, the demo does not work anymore. I’ll fix it this weekend.

      If you use the dgid > v0.3, which is uses dstore, then the grid can be filtered. Remember, the dgrid is only the visual representation of you data in the store. The filtering happens on the store.

  2. Hi Simon,

    Just wondering if this example is just for listing files under a directory or if it can actually open/launch those files that are displayed?

    Thanks,
    Kal

    1. Hi Kal

      The demo just lists the files, but all you would have to do is wrap them in an anchor tag and then let the request serve the appropriate file.

      I have a more elaborate example of a full file explorer you might be interested in: remoteFileExplorer, which is based on this example.

  3. Hi Simon,

    Great work here. Learning a lot from you on Dojo and DGrid. I almost got this example to work but nothing is loading in the dgrid. However, the left side tree with folders is loading fine. Any pointers on why that may happen would be very helpful. Been at it for 2 days but no hope yet.

    Thanks,
    Mark

    1. Without any code it’s hard to tell. A classic mistake is not to call startup() on the grid. Do you get a response from the server with data to populate the grid?

      It seems you are using a grid with a tree or the tree grid. I have a more recent demo of a dijit tree combined with a dgrid to create a file explorer. Maybe that code will help you.

      1. Hi Simon,

        Yes, i’m referring to your recent demo link itself where nothing is displayed for me in the grid. As for the startup(), I see only the panes startup is called in your example also. Please advise.

        Thanks,
        Mark

          1. Yep, I matched the server response to be of the same format as yours. The tree works fine with just displaying the directories (or even including the files). Nothing shows up on the dgrid although the code flow looks similar to your demo.

            Maybe as you said the problem is with version.. i’m using dgrid 0.4

            Thanks,
            Mark

          2. Another idea. I faintly remember, that I once had a CSS problem with the grid. The data was loaded, the grid and all rows were created, just the overflow/height was messed up so I didn’t see anything.

  4. Hi Simon,

    If not using a Restful Server, Can this still made to work with just changing from JsonRestStore to say dojox.data.FileStore or QueryReadStore??

    Thanks,
    Sid

    1. Hi Sid
      I just returned from a two months trip to Australia, so my head is not in programming mode at all. Also I haven’t dealt with dojo/store in a while. As far as I remember there is an adapter to make the dgrid work with legacy stores, but I never used it myself. To work with static data just use the store/memory. Do you now sitepens dgrid tutorials: http://dgrid.io/tutorials/0.4/grids_and_stores/

  5. Hi,

    I recently started learning dojo and dgrid and was looking for a demo on dgrid and came across your article. Great Demo and really learnt a lot. I was looking to connect it to the legacy Spring MVC Service instead of Restful. I thought just replacing the storeMaster to use ObjectStore instead should do the trick but encountering a lot of issues. if you don’t mind and have the time, can you please guide on the steps. or just how to say get this working with static data on the client side itself?

    Many thanks and looking forward for your reply. Cheers!! -Sid

  6. Hi, your elaborate example made me finally understand the dojo store complications. I just ditched gridx for dgrid and your example made that a success!
    Thanks for sharing

    1. Thank’s for the hint, but that creates a problem. When I now call this.store.query(‘2/’) there is a GET request to controller.php/2/ as before, but I get the error: “no filter function 2/ was found in store”. If I call this.store.query({parId: 2}) instead, I GET controller.php/?parId=2 with no error, but that’s not the format my controller expects. It seems that Observable expects the query format of the cachingStore and not of the masterStore. Is that the intended behaviour?

  7. Very instructive example, thanks! I wonder however why did You use dgrid, not dojo datagrid. Are there any special reasons?

    1. Thanks for the compliment. I mainly used it for the reason that I was never happy with the DataGrid’s slow scrolling of long content and that it works with the new dojo/stores out of the box. I also like that the dgrid’s code is very lean and simple to use. I also was just curious to check it out.