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
350 stars 55 forks source link

Initializing with topic cache when using postbox.subscribe #15

Closed jakeatoms closed 11 years ago

jakeatoms commented 11 years ago

We're having an issue where our module that publishes on a topic is loaded and publishing before the module that subscribes is loaded. We'd like to be able to pass a boolean to postbox.subscribe indicating that it should initialize to the existing topic cache. Is that possible? Thanks!

rniemeyer commented 11 years ago

So, in this case you are not using the observable extensions, right? The subscribeTo extension includes a boolean to populate using the latest published value (this.firstName = ko.observable().subscribeTo("myNameTopic", true);).

If you are not using the extensions (just calling subscribe), then you can still access the topicCache off of the postbox object (ko.postbox or if using AMD whatever you decided to call the variable).

For example, ko.postbox.topicCache.myTopic.value will give you access to the last published value for the myTopic topic. You could choose to use this value at the point where you subscribe,

jakeatoms commented 11 years ago

That should work in the short term. Would you accept a pull request if I added additional arguments to subscribe so that it matches subscribeTo and if the value is true, it would invoke the given action (with topicCache[topic]) after calling the ko subscribe function?

rniemeyer commented 11 years ago

sure, yes that sounds reasonable.

jakeatoms commented 11 years ago

ok, great! I'll work on that. I'm new to Git, so it will be good real world practice :)

rniemeyer commented 11 years ago

This was completed in #17.