I am trying to load a component conditionally using an if-binding. However, I can't change the value of the observable that is assigned to the if-binding using the construct below since, I think, the functions that should change the observable are outside the view model. I initially did this because this is what's done in the sample in KnockoutJS's site:
define(["jquery", "knockout", "ko-postbox",
"text!./parent.html"], function($, ko, kopost, template) {
ko.postbox.subscribe("child-click", function(newValue) {
ParentViewModel.prototype.displayChildContent(newValue);
}, this);
function ParentViewModel() {
//
// I NEED TO CHANGE THIS FROM OUTSIDE THE VIEW MODEL
//
this.childClicked = ko.observable(false);
}
ParentViewModel.prototype.displayParentContent = function(value) {
switch (value.toLowerCase()) {
default:
break;
}
};
ParentViewModel.prototype.displayChildContent = function(value) {
switch (value.toLowerCase()) {
case "child1":
alert();
//
// I NEED TO CHANGE OBSERVABLE HERE
//
break;
default:
break;
}
};
return { viewModel: ParentViewModel, template: template };
});
Then I tried doing it this way: I put the functions inside the view model and then assign the view model in a variable. Notice the return value: I used "instance" instead of the usual "viewModel" because when I use "viewModel", an error appears "Uncaught Error: Component 'parent': Unknown viewModel value: [object Object]" and also, it is constructed twice.
define(["jquery", "knockout", "ko-postbox",
"text!./parent.html"], function($, ko, kopost, template) {
ko.postbox.subscribe("child click", function(newValue) {
model.displayChildContent(newValue);
}, this);
function ParentViewModel() {
var self = this;
self.childClicked = ko.observable(false);
self.displayChildContent = function(value) {
switch (value.toLowerCase()) {
case "child1":
alert();
self.childClicked(true);
break;
default:
break;
}
};
}
var model = new ParentViewModel();
return { instance: model, template: template };
});
When I do this, the if-binding does not work. The error is "Uncaught ReferenceError: Unable to process binding "if: function (){return childClicked }" Message: childClicked is not defined"
Here is the parent HTML:
<div id="container">
<!-- ko if: childClicked -->
<!-- ko component: {name: "parent"} --><!-- /ko -->
<!-- /ko -->
</div>