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

ObservableObject in typescript

2 Answers 304 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Roger
Top achievements
Rank 1
Roger asked on 30 Aug 2015, 11:10 PM

Hi, i want to do something like this in typescript:

class ViewModel extends kendo.data.ObservableObject {
    constructor(values) {
        super(values);
        super.init(this);
    }
    get property1() { return super.get('property1'); }
    set property1(value) { super.set('property1', value); }
}

and use like so

var vm = new ViewModel({property1: 'default value'});
kendo.bind($("#something"), vm);
vm.property1 = 'updated value';

 

The problem i have is that in the ViewModel class, the get/set methods lead me to a stack overflow situation when the object is bound. 
The call to super.get('property1')  winds up calling an expression method that looks like  d.property1  which winds up back in my call to the getter for property1, and around and around we go.

Any way around this?  Basically, what i want is an ObservableObject that has the various bound properties expressed as get/set members that can be validated at design time, without have to use the ObservableObject's get/set  methods that use the property name as an argument.

Make sense?  
Any suggestions?

Thanks.

roger

 

 

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Petyo
Telerik team
answered on 02 Sep 2015, 08:07 AM

Hello Roger,

a thing I may suggest is to prefix the keys from the constructor argument with underscore - this will make the fields _property1, thus avoiding the stack overflow error. 

Regards,
Petyo
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Roger
Top achievements
Rank 1
answered on 02 Sep 2015, 10:34 PM

i thought of that, but then thought that it might mess up uses in bindings and other scenarios, but now that i think about it again, that really seems like it should work, and....

sure enough, that does ​apper to do the trick.  at least for the simple cases i've tried so far.

thanks for the suggestion

Tags
MVVM
Asked by
Roger
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Roger
Top achievements
Rank 1
Share this question
or