knockout and INotifyPropertyChanged

110 views
Skip to first unread message

Liviu

unread,
Apr 2, 2013, 11:13:06 AM4/2/13
to knock...@googlegroups.com
Hi all,

I have javascript objects where members are real ES5 properties with getters and setters, that raise a propertychanged  event when any property is set.

I want to databind these object using knockout, WITHOUT changing the structure of my objects, or creating other wrappers manually.

Knockout is supposed to be very intrusive replacing the properties with functions. 

IS THERE A DOCUMENTED WAY of achieving unintrusive databinding with third party objects?

Thanks.

Dietrich Schulten

unread,
Apr 4, 2013, 2:35:00 AM4/4/13
to knock...@googlegroups.com
Yes, I have a similar problem. I want to observe the current state of a hierarchical statemachine (jsfsa). Is it possible for instance to make an observable that is a subscriber to some external event? I would like to do something like data-bind='visible: isInState('Editing') where isInState is a computed which gets updated whenever the Statemachine changes state.
Jsfsa allows to add listeners.

Denys Khanzhiyev

unread,
Apr 4, 2013, 4:16:34 AM4/4/13
to knockoutjs
Without patching knockout code it is impossible to get standard databinding to third party objects w/o wrappers.
You may either:
1. Fork knockout and implement your own dependency tracking
2. Create some wraper factory - similar to knockout.mapping plugin
3. Create custom bindings for your objects like this:

ko.bindingHandlers['ES5text'] = {
   init:function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){
       var myObservableObjectName = valueAccessor(),
           observable = ko.observable(bindingContext[myObservableObjectName]) ;
bindingContext.$data.watch(myObservableObjectName,function(prop,old,new){
             observable(new);
       });
       ko.utils.domData.set(element,'ES5observable',observable);
   },
   update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){     
ko.bindingHandlers['text'].update(element, function(){
          return ko.utils.domData.get(element,'ES5observable';
       }, allBindingsAccessor, viewModel, bindingContext)  
}
}
not tested though - just for illustration of idea.



2013/4/4 Dietrich Schulten <dietrich...@googlemail.com>
Yes, I have a similar problem. I want to observe the current state of a hierarchical statemachine (jsfsa). Is it possible for instance to make an observable that is a subscriber to some external event? I would like to do something like data-bind='visible: isInState('Editing') where isInState is a computed which gets updated whenever the Statemachine changes state.
Jsfsa allows to add listeners.

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



dunc...@comcast.net

unread,
Oct 30, 2014, 7:38:05 AM10/30/14
to knock...@googlegroups.com
In this example, what would the binding look like in html?
If you do a direct <input type="text" data-bind="ES5text: propertyName"></input>, you don't get "propertyName" in valueAccessor, you get the value of whatever propertyName is in the model.

I used this as an inspiration to do some javascript object parsing, and build a binding with options instead of an actual property to do what i want, but it feels hacky.  am hoping someone has a better method on how to handle this.
Reply all
Reply to author
Forward
0 new messages