ko.mapping.fromJS(myObject) to a viewModel.If i use ko.applyBindings(viewModel); it updates the ui perfectly. But it adds the same events again. So if the user clicks the button, the event gets fired twice, third and so on.
var viewModel;
function update() 
{
    $.ajax({
        url: '...',
        type: "GET",
        statusCode: {
            200: function (data) {
                 viewModel = ko.mapping.fromJS(data);
                 ko.applyBindings(viewModel);
            }
        }
    });    
}
// first call after page load
update();
// user click
$("#myButton").click(function() {
    update(); 
});
Steve Greatrex Could you post your custom binding implementation?
ko.bindingHandlers.domBinding = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        viewModel.domElement = element;
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        viewModel.domElement = element;
    },
};
                var viewModel = ko.mapping.fromJS(data); This automatically creates observable properties for each of the properties on data .
KnockoutJS is basically a library written in JavaScript, based on MVVM pattern that helps developers build rich and responsive websites.
For example, ko. applyBindings(myViewModel, document. getElementById('someElementId')) . This restricts the activation to the element with ID someElementId and its descendants, which is useful if you want to have multiple view models and associate each with a different region of the page.
If you look at the 'Specifying the update target' on the mapping documentation, you can specify a target for the mapping.
This should mean that you can say:
if (!viewModel)
   viewModel = ko.mapping.fromJS(data);
else
   ko.mapping.fromJS(data, {}, viewModel); 
This way you will create a view model on the initial load, then update that view model for any subsequent loads.
Using mapping plugin I did this
ko.mapping.fromJS(JsonResponse, myObservable());
That's all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With