I'm getting an array of object from the server that I'd like to add a method to. In the sample below I'd like it to be called doAlert. When I try to bind to the doAlert using data-bind="click: doAlert" knockout throws an exception. I suspect I'm adding the function wrong so any advice would be greatly appreciated. Thanks
$(document).ready(function () {
var url = GetUrl();
$.getJSON(url, function (data) {
var mapping = {
doAlert: function (options) {
alert('test');
}
}
var viewModel = ko.mapping.fromJS(data, mapping);
ko.applyBindings(viewModel);
});
});
<ul data-bind="foreach: $data">
<li>
<span data-bind="text: Title"></span>
<div class="btn-group" style="display:inline">
<a class="btn-small btn dropdown-toggle" data-toggle="dropdown" href="#">Action
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<a data-bind="click: doAlert"><li>Edit</li></a>
<a><li>Delete</li></a>
</ul>
</div>
</li>