Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:51:06+00:00 2026-06-13T01:51:06+00:00

I have the following view model, used for editing Person objects in an HTML

  • 0

I have the following view model, used for editing Person objects in an HTML form:

function PersonModel(person) {
    var self = this;
    self.id = ko.observable(person.Id);
    self.firstName = ko.observable(person.FirstName);
    self.surname = ko.observable(person.Surname);
    self.email = ko.observable(person.Email);
    self.cell = ko.observable(person.Cell);

    self.save = function (data) {
        savePerson(data);
    };
}

When the user wants to edit a Person, I bind a new instance of this to an edit form, like this:

function editPerson(person) {
    var url = "@Url.Action("EditJson", "Person")";
    $.getJSON(url, function (data) {
        $("#person-detail").css("display", "block");
        ko.applyBindings(new PersonModel(data), $("#person-detail")[0]);
    });
}

And in the form I bind a click event to the save method in the view model:

<a href="#" data-bind="click: save">Update</a>

I now have a problem of multiple DB updates being run when I edit a single person record, and I assume this is because I have called applyBindings more than once on the same element, the edit popup. I can sort of confirm this as I have as many DB edits execute as times I call ApplyBindings.

Now I either need to know how to remove bindings applied by applyBindings, or how to only apply bindings once, and update my view model instead of recreating it for each edit. I would much prefer the first approach. A view model should not exhibit singleton characteristics.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:51:07+00:00Added an answer on June 13, 2026 at 1:51 am

    Ryan’s solution is the best for most situations. If you’re truly dealing with a module of JS and HTML code that can be instantiated multiple times on a page, you might want to look into ko.cleanNode() or ko.removeNode().

    Both of those methods are used internally by KO to handle memory leaks and binding cleanup. In a recent project, I have a table with several dynamically added rows of products. Each one of those products has to be editable inline. Instead of duplicating the form html for each row, I opted to setup a sort of KO module pattern where the html is dynamically loaded into the table row, bindings are applied, and bindings are removed once you’re done editing.

    Here’s the basic AMD module I inherit from:

    /**
    Prototype object for creating individual KnockoutJS modules
    
    @module Shared
    @class ko-module-prototype
    @namespace
    @static
    **/
    define(["knockout", 'jquery', 'Shared/js/helpers'], function (ko, $, helpers) {
        var exports = {};
        /**
        KO viewModel
        @property vm
        @static
        **/
        exports.vm = {};
        /**
        KO bindings
        @property bindings
        @static
        **/
        exports.bindings = {};
        /**
        Optional binding namespace for avoiding bindings getting overwritten
        @property bindingNamespace
        @static
        **/
        exports.bindingNamespace = null;
        /**
        Whether bindings are registered with KO or not
        @property bindingsRegistered
        @static
        **/
        exports.bindingsRegistered = false;
        /**
        The HTML node wrapping the area we wish to apply KO bindings
        @property $wrapperNode
        @static
        **/
        exports.$wrapperNode = $("body");
        /**
        Optional html string to be loaded in as a template
        @property template
        @static
        **/
        exports.template = null;
        /**
        Injects html, registers bindings, and applies bindings
        @method start
        @static
        **/
        exports.start = function ($wrapperNode) {
            // Update wrapper node
            if ($wrapperNode) this.$wrapperNode = $wrapperNode;
    
            this._openNodeId = "mod_" + helpers.uniqueId();
            var $targetNode = $wrapperNode;
    
            // Insert template html
            // Wrap in a div
            if (this.template) {
                this.$wrapperNode.html($("<div></div>").attr("id", this._openNodeId).html(this.template));
                $targetNode = $wrapperNode.find("#" + this._openNodeId);
            }
    
            // Register bindings
            // Class binding provider has to be setup first...
            if (!this.bindingsRegistered) {
                this.bindingsRegistered = true;
                var register = this.bindings;
                if (this.bindingNamespace !== null) {
                    register = { };
                    register[this.bindingNamespace] = this.bindings;
                }
                ko.bindingProvider.instance.registerBindings(register);
            }
    
            ko.applyBindings(this.vm, $targetNode[0]);
        };
        /**
        Removes html, and un-applies bindings
        @method end
        @static
        **/
        exports.end = function () {
            var $openNode = $("#" + this._openNodeId)[0];
    
            if (this.template !== null) {
                ko.removeNode($openNode);
            } else {
                ko.cleanNode($openNode);
            }
        };
        return exports;
    });
    

    It uses Ryan’s KO classBindingProvider which you can find here:

    https://github.com/rniemeyer/knockout-classBindingProvider

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model, view and controller. Model public class Person { public
In my view I have the following code: @using (Html.BeginForm()) { @Html.TextBox(Date2, Model.Date2) <br/>
in a partial view I have the following: <%Html.RenderAction(MVC.User.GetComments(Model.UserGroupName)); %> can I render a
I have the following code in a view: <div class=wrap_select> @Html.DropDownList(dateRange, new SelectList(Model.DateRange, Value,Text),
i have used the following code in my view and made the model it
I have been following the modified Model View Controller example presented in this article
I have a following view model and it will be used by a search
I have the following view: @model MyModel1 @{ ViewBag.Title = Index; Layout = ~/Views/Shared/_Layout.cshtml;
I have following line code in my view: <td> Model.some_instance_method(args) </td> I would like
I have the following requirement. I submit a Model object to a view as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.