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

Set sub-objects of observable object

5 Answers 54 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Dr.YSG
Top achievements
Rank 2
Dr.YSG asked on 12 Dec 2013, 05:32 PM
Is it possible to have a statement like:

vm.set("SysInfo.Browser", "Chrome");

Where SysInfo is defined in the Observable object as:

vm = kendo.observable({
        SysInfo = {
        status: "starting",
        xhr2: "false",
        browser: "",
        databases: "",
        center: "",
        date: "",
        zoom: 0
    }

5 Answers, 1 is accepted

Sort by
0
Dr.YSG
Top achievements
Rank 2
answered on 12 Dec 2013, 05:37 PM
The reason I ask is because I have a data-bound template 

<div  data-bind="source: SysInfo" data-template="SysInfoTemplate">Remove All DB Data</div>


I tried to change the value of SysInfo data, but it was not updating. I was doing things like

var sysInfo  = vm.get("SysInfo");
sysInfo.Browser = "Chrome";
vm.set("SysInfo", sysInfo);

But the values in the bound template were not updating.

This do work if I put everything at the top of the vm (and use 

data-bind="source: this"



0
Alexander Popov
Telerik team
answered on 13 Dec 2013, 10:59 AM
Hi Yechezkal,

Yes, you can achieve this by using the set method, however I noticed that the observable is not declared properly. For convenience I prepared this small example - I hope it helps.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Dr.YSG
Top achievements
Rank 2
answered on 13 Dec 2013, 02:54 PM
I noticed this syntax in your jsbin:

vm.set("SysInfo.browser", newBrowser);
vm.SysInfo.set("status", "stopping");
Does that mean that I could also do:

vm.SysInfo.set("browser", newBrowser);
vm.SysInfo.set("status", "stopping");
and if one of the items in SysInfo is a ObservableArray (which is MVVM bound to some elements) could I update the entire array at runtime by:

vm.SysInfo.set("DataArray", new kendo.data.ObservableArray(serverReplyJSON));
Or will this kill the binding, and what I need to do is first empty the array, and then push each item onto the array (which will cause N change events!).

0
Accepted
Alexander Popov
Telerik team
answered on 13 Dec 2013, 03:55 PM
Hello again,

Yes, the following notations are interchangeable: 
vm.set("SysInfo.browser", newBrowser);
vm.SysInfo.set("browser", newBrowser);
Additionally all arrays within an observable object are instance of kendo.data.ObservableArray, so you could just set the DataArray like so:  
vm.SysInfo.set("DataArray", serverReplyJSON); //assuming that serverReplyJSON is a valid JavaScript array
You can find a small example that illustrates similar behavior here.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Dr.YSG
Top achievements
Rank 2
answered on 13 Dec 2013, 04:04 PM
Excellent. maybe this could be added as a note to the documentation for ObservableArray and ObservableObject?
Tags
MVVM
Asked by
Dr.YSG
Top achievements
Rank 2
Answers by
Dr.YSG
Top achievements
Rank 2
Alexander Popov
Telerik team
Share this question
or