0

I have modified the dojo tutorial at http://dojotoolkit.org/documentation/tutorials/1.10/store_driven_tree/demo/demo.html to read from a JsonRest store.

The problem is that the tree display doesn't update when I click "Add new child to selected item" e.g. on the root element, although the update worked in the original tutorial.

I have compared what dojo/store/Memory (from the original tutorial) and dojo/store/JsonRest return after the "put" request: Memory returns the id of the new object. JsonRest ends with "return xhr(...)", so it returns a Deferred instead of the new id, which seems not not be understood by the Observable. I can make it work, if I change dojo/store/JsonRest.js to end with:

  ...
  return xhr(...).then(function(data){
    return data.id;
  };
}

I hope there is a solution without modifying the dojo sources?!

Some more details follow:

This is the definition of my store instead of the original Memory store:

var governmentStore = new JsonRest({
  target : "http://localhost:8080/test/gov",
  getChildren : function(object) {
    return this.query({
      parent : object.id
    });
  }
 });
 var governmentStore = new Cache(governmentStore,new Memory({}));

(If I remove the Cache and use the JsonRest directly, even the modified JsonRest.js doesn't make the Tree update).

This is the reply from a PUT request to the json server:

{"name":"New Child", "id":0.7243958345}

Please help to allow a dijit/Tree to react on changes of the underlying JsonRest store without messing around with the dojo sources.

Thank you

Dominic

Dominic
  • 1
  • 1

2 Answers2

0

Try wrapping your JsonRest store with an Observable wrapper and seeing if that helps the tree update properly. Also make sure that the model of the tree is functioning properly as that is what should be handling when and where the tree updates by listening to the store.

var memStore = new Memory({});
var store = new Observable(memStore); //Use this store for your tree
var cacheStore = new Cache(governmentStore,memStore);

The idea here is that when you do a PUT, you should be putting into the cacheStore and not the governmentStore. The Cache will do a PUT on the governmentStore but also update the memStore when the PUT is complete which should then trigger the notify in the Observable and pass that information along to the tree.

Richard
  • 1,138
  • 1
  • 6
  • 11
  • The JsonRest IS wrapped with an Observable, if you check the source of the demo linked above. The only difference is that I have replaced the "governmentStore=new Memory(...)" in the demo with "governmenStore=new JsonRest(...); governmentStore=new Cache(governmentStore,...);". How would I test that the model is functioning properly? All checks show that the JsonRest store returns the same data as the original demo. – Dominic Jun 03 '15 at 07:33
  • As I wrote, the difference is that a "PUT" request to JsonRest returns a Deferred, whereas a "PUT" request to Memory returns the id of the new item. Apparently this is a bug or at least an incompatibility between dojo data stores. – Dominic Jun 03 '15 at 07:34
  • Yes you're right in that Observable and JsonRest don't do well together. I had forgotten that JsonRest doesn't have a query engine (since the server is expected to handle some of the normal query engine aspects). I think what you need to do here is put an Observable around the Memory since that is the actual store that is getting updated. I've updated my answer a bit to show what I think should work. – Richard Jun 03 '15 at 10:46
  • If I follow your suggestion, I get the "Error: dijit.tree.ObjectStoreModel: root query returned 0 items, but must return exactly one". There is no request whatsoever sent to the server, which is to be expected, because the tree operates on an empty Memory store. Unless somebody has a solution I will file a bug report and use modified dojo sources (see my original answer) as a workaround, although this will be a pain in the ass. – Dominic Jun 03 '15 at 12:01
0

Using jquery instead of dojo was the solution. I found that I could solve in a few hours of learning jquery all problems that occurred when using dojo. This is mostly due to the quality of the documentation of both libraries and also because dojo seems to have too many bugs to react on new bug reports.

Dominic
  • 1
  • 1