3
votes

I have a large hierarchy of real-time data stored on a server. The structure of the hierarchy never changes but the values change continuously. On my client, I replicate the hierarchy as a knockout model and keep it updated using repeated ajax calls.

At any one time, only a small fraction of the hierarchy will be displayed in my client so I'd like to only make ajax calls for the values currently displayed.

Is there some 'standard' way to either count the number of current subscribers to an observable or, better still, track each subscriber to test its visibility?

Thanks!


Thanks for the replies re getSubscriptions.Count(). That gives me one solution - to get the ajax polling function to search the viewmodel for subscribers each time it polls. What I'd really like to do though is to allow the polling function to be notified each time the number of subscriptions changes to/from zero, so it can start and stop polling - this will be more processor-efficient than searching the viewmodel each time. What would be perfect is if there could be a computed observable as a child of the main observable called, say "subscriptionsCount()" and the poller (or anyone else) could then subscribe to that.

Another way I've considered is to wrap my 'value' in a computed observable a la http://www.knockmeout.net/2011/06/lazy-loading-observable-in-knockoutjs.html.. This provides a mechanism to notify the poller when the item is required, but doesn't solve the issue of notifying the poller when it is no longer required.

1

1 Answers

16
votes

There is method getSubscriptionsCount() on observable

self.name = ko.observable("Mike");
//...
var numberOfSubscribers = self.name.getSubscriptionsCount();  

JSFiddle DEMO