3

What is the recommended method for removing all items from a Kendo UI MVVM ObservableArray?

First, I tried re-initializing the ObservableArray, but that caused problems in my app. Next, I tried setting the length of the ObservableArray to 0, but that also caused problems. Then, I put the pop() method in a while loop. So far, that seems to be working, but I'm wondering if that's the recommended method.

MikeWazz
  • 109
  • 4
  • 13
  • Assuming Kendo ObservableArray is similar to a standard JS array, then see: [How to empty an array in JavaScript?](http://stackoverflow.com/q/1232040/9664) – Metro Smurf May 27 '13 at 17:55

2 Answers2

5

Update 23/01/2016 - patriks (answer below mine) has discovered a .empty() method which appears to be undocumented which does what I have used as an inbuilt method:

console.log(searchResults.empty); //function(){this.splice(0,this.length)}

I would recommend using this approach instead.

Old answer:

Just had to solve this today, I got it working with this:

var searchResults = new kendo.data.ObservableArray(['A', 'B', 'C']);
...
searchResults.splice(0, searchResults.length); //empties array.

Fiddle: http://jsfiddle.net/KyleMuir/wJW6f/

Kyle Muir
  • 3,875
  • 2
  • 22
  • 27
2

Happened to stumble upon this old thread and just thought I'd add that there is an (undocumented?) empty() method for ObservableArrays that does the splicing for you.

Updated Kyles fiddle: http://jsfiddle.net/wJW6f/2/

patriks
  • 250
  • 1
  • 3