dijit/Tree is not updated when connected to a dojo/store/JsonRest

623 views Asked by At

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

2

There are 2 answers

0
Dominic On BEST ANSWER

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.

4
Richard On

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.