我結束了使用從這裏的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);
謝謝。任何關於淘汰賽的pub/sub的想法? http://www.knockmeout.net/2012/05/using-ko-native-pubsub.html – SB2055 2013-04-30 13:56:17
我剛剛看過你的鏈接,我覺得這也很有意思,我會在看後更加深入。但我認爲durandal事件模塊更易於使用,如果您使用的是durandal,則應該使用該模塊。感謝您的鏈接。 – jvrdelafuente 2013-04-30 17:17:34