Reputation: 75
Knockout is calling subscribe function before rendering the view even without beforechange
attribute, when used with arrayChange
param.
I have created JSfiddle because StackOverflow ignores debugger;
keyword in JavaScript code, and that is the easiest way to see this behavior.
JSfiddle
Is there any way to signal KnockOut to call subscribe function after render?
I need to do this with subscribe, it is used by a custom bind and can not be invoked by afterRender
or similar view handling functions.
Upvotes: 0
Views: 631
Reputation: 371
You could expose a method on your view model to create the subscription, then call it on the vm after calling ko.applyBindings:
function ViewModel(){
var self = this;
this.items = ko.observableArray([]);
this.push = function(){
self.items.push((Math.random(10)*10).toFixed(0));
console.log("PUSH");
}
this.subscribeAfterApply = function(){
this.items.subscribe(changes=>{
console.log("SUBSCRIPTION FIRED");
}, null, "arrayChange");
console.log("SUBSCRIBED");
}
}
var vm = new ViewModel();
ko.applyBindings(vm);
vm.subscribeAfterApply();
Upvotes: 1