delete item from a dojo.store.jsonrest

2019-05-31 17:57发布

问题:

I started with this tutorial http://dojotoolkit.org/documentation/tutorials/1.6/store_driven_tree/

after setting up my ServerSide Restfull Service everything is working so far. I made a contextmenu for the tree by:

<ul dojoType="dijit.Menu" id="contextMenu" style="display: none;">
<li dojoType="dijit.MenuItem" iconClass="dijitEditorIcon dijitEditorIconDelete" onclick="pages.remove(tn.item.id);">delete page</li>
</ul>

<script type="dojo/connect">
var menu = dijit.byId("contextMenu");
menu.bindDomNode(this.domNode);

dojo.connect(menu, "_openMyself", this, function(e){
    // get a hold of, and log out, the tree node that was the source of this open event
    tn = dijit.getEnclosingWidget(e.target);

    // contrived condition: disable all menu items except the "New Page" item
    dojo.forEach(menu.getChildren(), function(child){
        if(child.label != "Neue Seite")
        {
            child.set('disabled', typeof tn.item == 'undefined');
        }
    });
});
</script>

Now I know on wich node the user made the right click for the contextmenu and delete it with "pages.remove(tn.item.id);" from the Database. To notify the tree I´m overriding the remove function:

remove: function(objectId){

    this.onDelete({id: objectId});
    return dojo.store.JsonRest.prototype.remove.apply(this, arguments);

}

Everything works as expected but if im now doing some other things with the items in the tree like drag n drop an item to the root i was deleting a child before. The tree isn't showing it correctly anymore. I think the remove method of the store only sends the DELETE query to the Server but don't removes the item from the store. How can i get the array of the items in store to check and maybe to delete items?

回答1:

The dijit.Tree is a presentation of an underlying dojo.data model, and any changes that you want to make to the tree really need to be done to the underlying data store. See the description here: http://dojotoolkit.org/reference-guide/dijit/Tree.html#dijit-tree So, instead of overriding the remove function, you should instead use the dojo.data API to modify the store, and then rerender the tree to reflect the changes. The best source for looking at the various methods available is in the dojo nightly files. Specifically, the dojo.data files are here: http://archive.dojotoolkit.org/nightly/dojotoolkit/dojo/data/

var item = tree.fetchItemByIdentity("selectedItem");  //find the item you want to remove
store.deleteItem(item);               //delete the item from the data store
store.save();                         //save the change made to the store


标签: rest dojo store