ObservableObject in typescript

3 posts, 1 answers
  1. Roger
    Roger avatar
    17 posts
    Member since:
    Aug 2012

    Posted 30 Aug 2015 Link to this post

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

    class ViewModel extends kendo.data.ObservableObject {
        constructor(values) {
        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?






  2. Answer
    Petyo avatar
    2444 posts

    Posted 02 Sep 2015 Link to this post

    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. 

    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Roger
    Roger avatar
    17 posts
    Member since:
    Aug 2012

    Posted 02 Sep 2015 in reply to Petyo Link to this post

    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

Back to Top