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

Slice with Nested Observable Array

5 Answers 123 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 07 May 2014, 01:46 PM
I am trying to use the slice method on my Observable Array.  Each item in my array has another observable array.  The slice works great, as the objects returned are prefect "copies", but the Observable Arrays in each object are not copied, and therefor still reference the observable array from the item it was copied from.  How can I do a true deep copy for my items with any nested items that item may have?


Thanks and sorry if this sounds confusing.  

5 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 09 May 2014, 12:03 PM
Hi Joe,

Could you please show us the code that you are using and the objects that contain those arrays?

Regards,
Atanas Korchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Joe
Top achievements
Rank 1
answered on 09 May 2014, 12:33 PM
My objects are really large and complex, but I'll do my best to summarize.  I know th following code is not syntatically correct, but I hope you get the idea. If I were to run something like the following, I would expect to get a "copy" of my sub item from group 2 and then have that "copy" be altered an then pushed to group 1's sub items.  The problem I'm seeing is that when I alter my sliced subitem it is not a real copy and thus when I alter it, it is changing the original array item that was sliced.  I have gotten around this by stringifying my sliced object, parsing it, and then pushing it into my observable array.

Thanks for the help. 

viewModel = kendo.observable({
   groups: arrayOfGroups
});
 
arrayOfGroups = [
  {item : { key : 'item 1', items : [{ key : 'sub item 1'}]},
  {item : { key : 'item 2', items : [{ key : 'sub item 2'}, { key : 'sub item 3}]}
]
 
var subItem2 = viewModel.get('groups[1]').items.slice(0,1);
subItem2.key = 'new sub item';
viewModel.get('groups[0]').items.push(subItem2);
0
Atanas Korchev
Telerik team
answered on 09 May 2014, 01:27 PM
Hi Joe,

Indeed the slice method won't copy the ObservableArray. You can however clone it like this:

var clone = new kendo.data.ObservableArray(original.toJSON());


Regards,
Atanas Korchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Joe
Top achievements
Rank 1
answered on 09 May 2014, 01:32 PM
I wasn't expecting to copy the observable array, I was trying to take a slice from an obsevable array, but to your point my bigger issue is probably that the child observable array is not being "copied".

Thanks for the reply. 
0
Atanas Korchev
Telerik team
answered on 09 May 2014, 01:35 PM
Hello Joe,

I guess I still don't understand the issue. Could you please try reproducing it in http://trykendoui.telerik.com ? Once I have some working code I can suggest a workaround or solution.

Regards,
Atanas Korchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
MVVM
Asked by
Joe
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Joe
Top achievements
Rank 1
Share this question
or