rniemeyer / knockout-postbox

A small library that uses Knockout's native pub/sub capabilities to facilitate decoupled communication between separate view models or components.
MIT License
348 stars 55 forks source link

Does not work for computed #40

Open jim-phillips opened 8 years ago

jim-phillips commented 8 years ago

I am working on a complex price estimator. I have multiple sections each with multiple rows that can be dynamically added and removed, each with multiple components. It is completely data-driven, which is neither here nor there, but is a requirement. When any of the component inputs change, the row price, section price, and total price all must be updated. Further complicating the matter, the components are interdependent. Meaning, one component of a row may effect the price of 1 or more components in the row. I originally had this working by sending in the row to each of the individual components in order to find the related components that effected the price of another component. I found your module and thought it would be great for decoupling the components. However, due to the interdependencies, the price and other values require ko.computed values. When I use postbox, the row price does not update when a computed value changes. However, changing a ko.observable value does trigger the computed function to run. The bottom line is that the built in knockout binding works as long as I don't try to bind a postbox subscription to a computed value. When the computed function accesses a computed function bound to a subscribable, the computed function does not trigger. It only triggers when an observable value changes. That is why I think postbox does not work correctly with computed values. It seems to override, rather than augment the built-in KO functionality.

jim-phillips commented 8 years ago

I was just playing around and found something interesting. I was actually using pureComputed, and used ko.postbox.subscribe within the function. When I change it to computed, I get errors because the subscribe function does not have the subscribed value yet. I should also note that I am using v3.4.0 with ko.options.deferUpdates = true; set.

jim-phillips commented 8 years ago

I think I have a workaround for this, but it would require subscribing to multiple topics. Is that possible? I am thinking I could set the price as an observable and subscribeTo multiple topics, each triggering the function to change. It is not ideal, but I am in a time-crunch.