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

Multiple ObservableObjects referencing the same ObservableObject

3 Answers 228 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 06 Feb 2013, 10:24 PM
Hello,

I have run into a problem when trying to use several ViewModels referencing the same model.
ViewModels and models are all ObservableObjects.
Please, consider the following simple example:

    // observable for model
    var model = kendo.observable({ mf: 0 });      

    // observables for two viewmodels
    var vm1 = kendo.observable({ vmFld1: null });
    var vm2 = kendo.observable({ vmFld2: null });

    // set fields of both viewmodels to the same
    // observable model
    vm1.set("vmFld1", model);
    vm2.set("vmFld2", model);

Both ViewModels bind CHANGE event handlers in ObservableObject.wrap() method.
They are added to the array of CHANGE event handlers of the model and are to be triggered when model field is changed.
E.g. like in the line of code below

    // change the value of model field
    vm1.set("vmFld1.mf", 1);

These event handlers are invoked in a loop of Observable.trigger() method.
But each event handler modifies the e.field in event argument e.

wrap: function(object, field, parent) {
    var that = this;
    …
    (function(field) {           
            ...

            object.bind(CHANGE, function (e) {
                e.field = field +  "."  + e.field;
                that.trigger(CHANGE, e);
            });
    })(field);
    ...
}

It should work just fine for the case when we have a linear chain of nested ObservableObjects, but in my case it is rather a tree with single ObservableObject root. The second ViewModel gets incorrect "path" to changed field in CHANGE event handler.

In example above the first ViewModel gets "vmFld1.mf" in CHANGE event. Then it modifies the path in its handler and the second ViewModel vm2 gets incorrect path "vmFld2.vmFld1.mf". 

As a result, vm2 does not refresh view when View-ViewModel binding is used.

I'm not sure if you already know about this and it might be even already fixed in latest version. But I can look only into available for download open source code which is of 2012.3.1114 version.

I very appreciate if you can resolve this issue. Thank you.

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 10 Feb 2013, 10:40 PM
Hello Alex,

This issue should be resolved in the service pack release which is available for you to download from your account. The event argument is now cloned so the path should be correct. 

Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Alex
Top achievements
Rank 1
answered on 12 Feb 2013, 06:32 PM
Hi Daniel,
Thank you for good news!
Sorry, I don't quite understand you and excuse me my (may be silly) question. Which service park you are talking about? Is it official Q3 2012 SP1 (version 2012.3.1315) - January 2013 ? Or is it some special service pack available from my account only for me?
Actually our company is considering to buy Kendo UI Web. Will this fix be available in commercial version?

Thank you.
0
Daniel
Telerik team
answered on 14 Feb 2013, 06:21 PM
Hello Alex, 

The problem should be fixed in the service pack release(2012.3.1315) that is available for download for all licensed and trial users. The changes will also be available in the next official release.

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