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

  • SEARCH
  • Home
  • 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 7433925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:45:03+00:00 2026-05-29T09:45:03+00:00

Looking for a good example of how to set up child models in knockoutjs.

  • 0

Looking for a good example of how to set up child models in knockoutjs. This includes binding to child events such as property updates which I haven’t been able to get working yet.

Also, it would be better to bind to a single child in this case instead of an array but I don’t know how to set it up in the html without the foreach template.

http://jsfiddle.net/mathewvance/mfYNq/

Thanks.

<div class="editor-row">
    <label>Price</label>
    <input name="Price" data-bind="value: price"/>
</div>

 <div class="editor-row">
    <label>Child</label>
    <div data-bind="foreach: childObjects"> 
        <div><input type="checkbox" data-bind="checked: yearRound" /> Year Round</div>
        <div><input type="checkbox" data-bind="checked: fromNow" /> From Now</div>
        <div>
            <input data-bind="value: startDate" class="date-picker"/> to 
            <input data-bind="value: endDate" class="date-picker"/>
        </div>
    </div>
</div>
var ChildModel= function (yearRound, fromNow, startDate, endDate) {
    var self = this;

    this.yearRound = ko.observable(yearRound);
    this.fromNow = ko.observable(fromNow);
    this.startDate = ko.observable(startDate);
    this.endDate = ko.observable(endDate);

    this.yearRound.subscribe = function (val) {
        alert('message from child model property subscribe\n\nwhy does this only happen once?');

        //if(val){
        //    self.startDate('undefined');
        //    self.endDate('undefined');
        //}
    };
}

var ParentModel = function () {
    var self = this;

    this.price = ko.observable(1.99);
    this.childObjects = ko.observableArray([ new ChildModel(true, false) ]);
};

var viewModel = new ParentModel ();
ko.applyBindings(viewModel);
  • 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-05-29T09:45:03+00:00Added an answer on May 29, 2026 at 9:45 am

    Try it with the following:

    this.yearRound.subscribe(function (val) {
            alert('value change');
        });
    

    If you want to have the subscriber also being called while loading the page do something like this:

    var ChildModel= function (yearRound, fromNow, startDate, endDate) {
        var self = this;
    
        this.yearRound = ko.observable();
        this.fromNow = ko.observable(fromNow);
        this.startDate = ko.observable(startDate);
        this.endDate = ko.observable(endDate);
    
        this.yearRound.subscribe(function (val) {
            alert('value change');
        });
        this.yearRound(yearRound);
    }
    

    http://jsfiddle.net/azQxx/1/ – this works for me with Chrome 16 and Firefox 10

    Every time the checked button changes its value the callback fires.

    The observableArray is fine in my opinion if you may have more than one child model associated to the parent.

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

Sidebar

Related Questions

I'm looking for a good source code example of a supervised neural network which
I'm looking for a good example of storing data in a cookie using ASP.NET.
I'm looking for a good example that shows how to handle (in the controller)
I am looking for a good example of how to achieve the following: I
I'm looking for a good example of using Regular Expressions in PHP to reverse
I am looking for good methods of manipulating HTML in PHP. For example, the
I'm looking for (simple) examples of problems for which JMS is a good solution,
EDIT: People keep visiting this post which doesn't have much good information so I'll
I'm looking for a good Java BitSet example to work with 0 and 1s.
Problem: I am looking for good introduction about type systems , which are based

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.