waigani
waigani

Reputation: 3580

dojo.data.objectStore.deleteItem

I have a dojo.store.Memory wrapped in a dojo.data.ObjectStore which I am then plugging into a dataGrid. I want to delete an item from the store and have the grid update. I have tried every combonation I can think of with no success. For example:

var combinedStore = new dojo.data.ObjectStore({objectStore: new dojo.store.Memory({data: combinedItems})});
            combinedStore.fetch({query:{id: 'itemId'}, onComplete: function (items) { 
                var item = items[0];
                combinedStore.deleteItem(item);
                combinedGrid.setStore(combinedStore);
            }});
            combinedGrid.setStructure(gridLayout);

This throws no errors but combinedStore.objectStore.data still has the item that was meant to be deleted and the grid still displays the item. (The also seems to be a complete mismatch between combinedStore.objectStore.data and combinedStore.objectStore.index);

Upvotes: 2

Views: 3871

Answers (2)

Neek
Neek

Reputation: 7461

This does seem rather poorly documented at present in the new dojo.store documentation.

The old dojo.data.api.Write documentation make it fairly clear. An excerpt from http://dojotoolkit.org/reference-guide/dojo/data/api/Write.html:

Datastores that implement the Write interface act as a two-phase intermediary between the client and the ultimate provider or service that handles the data. This allows for the batching of operations, such as creating a set of new items and then saving them all back to the persistent store with one function call.

The save API is defined as asynchronous. This is because most datastores will be talking to a server and not all I/O methods for server communication can perform synchronous operations.

Datastores track all newItem, deleteItem, and setAttribute calls on items so that the store can both save the items to the persistent store in one chunk and have the ability to revert out all the current changes and return to a pristine (unmodified) data set.

Revert should only revert the store items on the client side back to the point the last save was called.

dojo.store has evolved from dojo.data and seems to follow many of its behavioral aspects.

The new dojo.store documentation http://www.sitepen.com/blog/2011/02/15/dojo-object-stores/ and http://www.sitepen.com/blog/2011/02/15/dojo-object-stores/ manages to talk specifically about the delete operation without mentioning having to call save() (in fact I can't find the word 'save' on that page at all).

I'm staying away from dojo.store as long as possible, hopefully it will be easier to follow in 1.7 or later, whenever I'm forced to use it for real :)

Upvotes: 1

OverZealous
OverZealous

Reputation: 39570

There's a simple solution, luckily! The delete is successfully happening, however, you need to save the ObjectStore after the deletion for it to be committed.

Change your code to look like this:

onComplete: function (items) { 
    var item = items[0];
    combinedStore.deleteItem(item);
    combinedStore.save();
    combinedGrid.setStore(combinedStore);
}

That little save should do the trick. (Please note: the save must occur after the deleteItem - if you put it outside the fetch block, do to being asynchronous, it will actually happen before the onComplete!)

Working example: http://pastehtml.com/view/b34z5j2bc.html (Check your console for results.)

Upvotes: 2

Related Questions