dojodgrid

How can I remove all items from a Dojo dstore?


I am having trouble removing all items from a dstore. I am trying this, which seems like it should work, but it fails at the end:

var TrackableMemory = declare([ Memory, Trackable ]);
var userMem = new TrackableMemory({
    data: {the data...},
    idProperty: '_id'
});    
userMem.forEach(function (userObj) {
    userMem.remove(userObj._id);
});

I put up a working (or not working, rather) example in this fiddle. See the console for the "cannot read property '_id' of undefined" error when it can't find the last record.

I have other things connecting to this store instance, so I can't really just reset everything by redefining userMem.

What am I doing wrong? How can I remove all items from a dstore?


Solution

  • Turned out to be a simple JS array problem of modifying the array over which I was iterating. Looping backwards over the array with a simple for works:

    userMem.fetch().then(function (users){
        for (var i = users.length - 1; i >= 0; i--) {
            userMem.remove(users[i]._id);
        }           
    });