This is a migrated thread and some comments may be shown as answers.

Working with arrays

3 Answers 704 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 02 Mar 2012, 08:36 PM
How are you supposed to work with arrays? I am messing with the "elements.html" example provided in the download for the beta and when I try to change the array of fruits to a new array, it doesn't work correctly (places that bind to it show the array as a comma-separated list instead of as distinct values).

In the example, I added this method to the viewModel (bound to a button)...

    changeStuff: function(e) {
        e.preventDefault();
        this.set("fruits", ["Pear", "Peach", "Plumb"]);
    }

When I click the button, the "Select" and "Multiple select" lists are updated, but instead of individual lines for each fruit, it shows all the fruits on the same line separated by commas.

Also, how would I just change a single value in the list? For example, change "Banana" to "Pineapple"?

3 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 05 Mar 2012, 09:56 AM
Hi, 

Arrays in Kendo MVVM are implemented by the kendo.data.ObservableArray type. Since inheriting from JavaScript arrays is not possible cross-browser we implemented an array-like interface in ObservableArray. The following methods are supported:

  • push
  • pop
  • slice
  • splice
  • shift
  • unshift

If you want to replace an item in an ObservableArray you need to use the splice method:

viewModel.fruits.splice(1, 1, "Pineapple");

This should have also worked:

this.set("fruits", ["Pear", "Peach", "Plumb"]); 

however the set method does not make its argument observable in the Beta. We should make this work in the official release.

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Joshua
Top achievements
Rank 1
answered on 08 Jul 2012, 02:32 PM
I tried push on my child node items array form my treeview in my viewmodel and it did not show up in the treeview.
However if I used the same code to push before I called my databind method then it DID show up.

However I need this to work after I have bound.
0
richard
Top achievements
Rank 1
answered on 24 Jul 2012, 04:27 AM
This was the best solution i could come up with, hopefully it helps someone

 var _tempObservableArray = new kendo.data.ObservableArray(this.get("observableArray"));
 _tempObservableArray.push(this.get("newValue"));  this.set("observableArray", _tempObservableArray); 
Tags
MVVM
Asked by
Brian
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Joshua
Top achievements
Rank 1
richard
Top achievements
Rank 1
Share this question
or