<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<h1>Component Test</h1>
<p>
The component contains a dialog , which we
want to show based on events on the main viewmodel.
There is no way to invoke a method on a component, so
I have to pass an observable to be able to mimic this.
</p>
<button data-bind="click: Find">Find Customer</button>
<p>Result: <span data-bind="text: Result"></span> </p>
<hr/>
Component:
<search-component
params="show: DialogVisible, callback: Update">
</search-component>
JavaScript
ko.components.register("search-component", {
viewModel: function(params) {
var self = this;
self.Show = params.show;
self.Callback = params.callback;
self.Submit = function() {
// hide dialog
self.Show(false);
// return result
self.Callback("result");
};
},
template:
"<div data-bind='visible: Show'>" +
"<p>search dialog goes here</p>" +
"<button data-bind='click: Submit'>Submit</button>"+
"</div>"
});
// top level viewmodel
var vm = function() {
var self = this;
self.Result = ko.observable("");
self.DialogVisible = ko.observable(false);
// user has clicked Find button
self.Find = function() {
// show the dialog
self.DialogVisible(true);
};
// callback
self.Update = function(data) {
self.Result(data);
};
};
ko.applyBindings(new vm());
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.