rniemeyer / knockout-classBindingProvider

An alternate binding provider for Knockout that allows you to define your bindings in a JavaScript object and key into it from the markup.
MIT License
101 stars 27 forks source link

data-class function is called multiple times in KO 3.0 #18

Closed lisovin closed 10 years ago

lisovin commented 10 years ago

This is best can be demonstrated by following jsbin-s - same exact code results in different behavior in KO 2.3 and 3.0:

KO 2.3: http://jsbin.com/ULovitA/3 (number of calls is 1) KO 3.0: http://jsbin.com/ULovitA/2 (number of calls is 4)

I suspect the issue is related to the change in KO 3.0 that now evaluates every binding independently (and thus calling binding function multiple times). I wonder what a good workaround might be though.

mbest commented 10 years ago

classBindingProvider would need to be updated to use getBindingAccessors.

rniemeyer commented 10 years ago

yes- that has been on my list of things to look at (providing getBindingAccessors for 3.0 support).

mbest commented 10 years ago

I worked on a prototype for this back in August. I pulled that out again and make some tweaks to get it working optimally with Knockout 3.0. See #19.

mbest commented 10 years ago

In general, you'll get the most benefit from Knockout 3.0 if you pass the observables themselves to bindings instead of unwrapping them in your bindings.

Thus you should have

      counter: function () {
        return {
          text: this.counter
        };
      }

instead of

      counter: function () {
        return {
          text: this.counter()
        };
      }
lisovin commented 10 years ago

@rniemeyer: New version of classBindingProvider seems to have resolved the issue. Thanks a lot!

lisovin commented 10 years ago

@mbest: your point regarding passing observables rather than unwrapping them in the binding is well taken, thanks for the advice.