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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:54:43+00:00 2026-06-15T17:54:43+00:00

I have a viewmodel as follow : var PostModel = function(data){ ko.mapping.fromJS(data, {}, this);

  • 0

I have a viewmodel as follow :

    var PostModel = function(data){
        ko.mapping.fromJS(data, {}, this);
    }; 

var vm = (function () {
    var post = {};
    var posts   = ko.observableArray([]);
    var getPost = function () {
        var tmp = {
            title: new Date(),
            content: new Date()
        };
        //post.title(tmp.title());
        //post.content(tmp.content());
        ko.mapping.fromJS(tmp, mappingOption, post);

    };
    var getPosts = function () {
        posts.removeAll();
        for (var i = 0; i < 2; i++) {
            posts.push({ title: new Date() });
        }
    };

    var mappingOption = {
        create: function (options) {
            return new PostModel(options.data);
        }
    }

    return {
        post    : post,
        posts   : posts,
        getPost : getPost,
        getPosts: getPosts
    };
})();

ko.applyBindings(vm);​

​
and responding html as follow :

<button data-bind="click: getPost">get post</button>
<br />
<label data-bind="text: post.title" ></label>
<br />
<button data-bind="click: getPosts">get post list</button>
<ul data-bind="foreach: posts">
    <li data-bind="text: title"></li>
</ul>

I expected post object and posts array object would updated when getPost and getPosts function excuted, however post object is not updated in the html while posts array object updated in the html.

JsFiddle is here (http://jsfiddle.net/outia24/AVygn/)

Did I miss anything?

  • 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-15T17:54:44+00:00Added an answer on June 15, 2026 at 5:54 pm

    When you have an observable, such as:

    var post    = { title: ko.observable('test') } ;
    

    You can update the value of the title like this:

    post.title("New Title");
    

    If you replace the title or if you replace post after the bindings are set up, then the binding get disconnected and you lose the binding. Here are some more samples:

    var vm = (function() {
       var self = this;
       self.post = { title: ko.observable("test") } ;
       return self;
    })();
    
    // This will cause "test" to show up in the HTML    
    ko.applyBindings(vm);
    
    // Update the title in the viewmodel and HTML to "New Title"
    // This is the proper way to update an observable.
    vm.post.title("New Title");  
    
    // If you want to retrieve the value of an observable, you call
    // it like a function, like this
    var TheTitleOfMyPost = vm.post.title();
    
    // This will break the binding, because you are creating a new
    // object and pointing the post variable to the new object.
    // The HTML, however, is still bound to the old object.
    // Don't do it this way.
    post = { title: ko.observable("Doesn't Work") };  // Initial Set
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up question to this post I have two models: CarType.cs
I have a user control let say UC1 . This user control have viewmodel
I have a viewmodel which is subscribing to the event NavigationCompletedEvent. This viewmodel need
I have this ViewModel public class CustomerSuscribeViewModel : IValidatableObject { [DataMember(IsRequired = true)] [DataType(DataType.Text)]
Are there any good practices to follow when designing a model/ViewModel to represent data
This is a follow up to a question that was asked yesterday. I have
Let's say we have data as follows var data = { facets: [{ name
I am trying to implement Repository pattern in JavaScript. I have ViewModel which i
Hi people stackoverflow. I'm working with MVVM, I have ViewModel call UserViewModel with a
I have a ViewModel I am binding to a view: ProductViewModel model = Mapper.Map<Product,

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.