我最终从这里使用了 pub/sub:knockmeout.net/2012/05/using-ko-native-pubsub.html
我的小提琴:http:
//jsfiddle.net/PTSkR/38/
/* Pub/sub code */
var postbox = new ko.subscribable();
ko.subscribable.fn.publishOn = function(topic) {
this.subscribe(function(newValue) {
postbox.notifySubscribers(newValue, topic);
});
return this; //support chaining
};
ko.subscribable.fn.subscribeTo = function(topic) {
postbox.subscribe(this, null, topic);
return this; //support chaining
};
/* Selection code */
this.selectedItems = ko.observableArray().subscribeTo("TOPIC");
var viewModel = {
documents: [{"documentId": "1"}, {"documentId": "2"}, {"documentId": "3"}],
selectedDocumentIds: ko.observableArray()
};
viewModel.documentIndex = {};
ko.utils.arrayForEach(viewModel.documents, function(doc) {
viewModel.documentIndex[doc.documentId] = doc;
});
this.selectedDocuments = ko.computed(function() {
return ko.utils.arrayMap(viewModel.selectedDocumentIds(), function(id) {
return viewModel.documentIndex[id];
});
}).publishOn("TOPIC");
ko.applyBindings(viewModel);