2

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?

James Irwin
  • 1,171
  • 8
  • 21
  • Ah, seems like this might just be a JS array problem. e.g., `var a = ['a', 'b', 'c', 'd']; a.forEach(function (letter) {var ind = a.indexOf(letter); a.splice(ind, 1);});`. Now `a` is `['b', 'd']`. Looks like this is what dstore is doing with its `forEach` function and Memory's `removeSync`? – James Irwin Feb 16 '15 at 20:45

2 Answers2

2

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);
    }           
});
James Irwin
  • 1,171
  • 8
  • 21
0

This worked for me

// Forget all data
myGrid.store.data = [];

// Refresh the grid
myGrid.refresh();
Deejers
  • 3,087
  • 2
  • 23
  • 20