Note that there are some explanatory texts on larger screens.

plurals
  1. POObservable arrays with knockout publishing/subscribing
    primarykey
    data
    text
    <p>I'm following this post: </p> <p><a href="http://www.knockmeout.net/2012/05/using-ko-native-pubsub.html" rel="nofollow">http://www.knockmeout.net/2012/05/using-ko-native-pubsub.html</a></p> <p>to allow one of my vms to communicate with the other. One of my viewModels has an array that I need to expose to another viewModel. If this array is not an observable, the method from the above post works just fine. </p> <p>If the array is an observable array, publishedSelectedFolders is never populated. I'm trying to figure out why; hopefully it's something silly I'm doing. </p> <p>Here's my jsFiddle: </p> <p><a href="http://jsfiddle.net/PTSkR/40/" rel="nofollow">http://jsfiddle.net/PTSkR/40/</a></p> <p>If you uncomment the line in the vm, it works as expected (publishedSelectedFolders is populated as checks are ticked). Why is this happening?</p> <p>Code: </p> <pre><code> /* * Pub/Sub (decouples VMs but lets them access each others' data) */ 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.publishedSelectedFolders = ko.observableArray().subscribeTo("SELECTED_FOLDERS"); var vm = { folders: ko.observableArray([{ "folderId": "1" }, { "folderId": "2" }]), // folders: [{"folderId": "1"}, {"folderId": "2"}, {"folderId": "3"}], selectedFolderIds: ko.observableArray(), }; vm.folderIndex = {}; ko.utils.arrayForEach(vm.folders, function (folder) { vm.folderIndex[folder.folderId] = folder; }); /* monitors selections and publishes to the shell */ this.selectedFolders = ko.computed(function () { return ko.utils.arrayMap(vm.selectedFolderIds(), function (id) { return vm.folderIndex[id]; }); }).publishOn("SELECTED_FOLDERS"); ko.applyBindings(vm); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload