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 8772559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:03:48+00:00 2026-06-13T18:03:48+00:00

I’m attempting to gradually sprinkle KnockoutJS into my existing application. My first stab at

  • 0

I’m attempting to gradually sprinkle KnockoutJS into my existing application. My first stab at this is to take some small existing forms and have their values pushed to the server on blur as well as updating some elements via binding.

The issue I’m running into is that when the form is first displayed it is not being populated via knockout. I’m rendering the page with the data already in the form. So I setup my knockout like this:

function DomainViewModel() {
  this.name = "";
  this.description = "";
}

ko.applyBindings(new DomainViewModel());

And I have my form like so:

<input data-bind="value: name" value="${domainInstance.name.encodeAsHTML()}"/>
<textarea data-bind="value: description" >${domainInstance.description.encodeAsHTML()}</textarea>

So what is happening is my form is displayed and then knockout applies the values from the ViewModel to the form, which wipes out the values that were put there by the server. I understand why this happens and that this is not a bug. However, I’m wondering if there is another option here.

I know that I could do something like the following:

function DomainViewModel() {
  this.name = "${domainInstance.name}";
  this.description = "${domainInstance.name}";
}

But that would require me to put some of the javascript directly in the GSP (I’m using Grails) rather than an external script file.

  • 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-13T18:03:50+00:00Added an answer on June 13, 2026 at 6:03 pm

    So, you have a few ways you could do this. First, never do this

    function DomainViewModel() {
      this.name = "${domainInstance.name}";
      this.description = "${domainInstance.name}";
    }
    

    Your viewmodel has just become un-reusable. If you want to use grails to populate the viewmodel, set some javascript object on the page, and pass that into your external javascript file viewmodel definition, like this (don’t forget to make your properties obsevables!):

    var grailsData = {
        name : "${domainInstance.name}",
        description : "${domainInstance.name}"
    };
    
      ...
    function DomainViewModel(data) {
      this.name = ko.observable(data.name);
      this.description = ko.observable(data.description);
    }
    
    ko.applyBindings(new DomainViewModel(grailsData);
    

    I would recommend this approach, because later when you change the source of the data, you don’t have to alter the HTML. However, there is another option. You could create a binding that collected the value from the elements and use it to set the initial observable values. That binding might look like this:

    ko.bindingHandlers.valueWithInit= {
        init: function(element, valueAccessor) {
            valueAccessor()(element.value);
        },
        update: function(element, valueAccessor) {
            var value = valueAccessor();
            element.value = ko.utils.unwrapObservable(value);
        }
    };
    

    Here is a fiddle demonstrating this method in action

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.