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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:05:43+00:00 2026-05-19T04:05:43+00:00

Please consider the following view: <p>Count <span data-bind=text: unreadCount()>&nbsp;</span></p> <div data-bind=’template: { name: conversationTemplate,

  • 0

Please consider the following view:

<p>Count <span data-bind="text: unreadCount()">&nbsp;</span></p>
<div data-bind='template: { name: "conversationTemplate", data: currentList }'> </div>
<script type="text/html" id="conversationTemplate">
 <table>
  <tbody>
   {{each $data}}
    <tr id="conversation_${conversation_id}" class="conversation-item ${status}">
     <td><input type="checkbox" data-bind="click: function() { alert(this.value) }, checked: read" /></td>
    </tr>
   {{/each}}
  </tbody>
 </table>
</script>

And the following code:

$(function() {

 var viewModel = {
  currentList : ko.observableArray([])
 };

 ko.dependentObservable(function () {
  $.ajax({
   url: '/conversations/inbox.json', dataType: 'json',
   success: function(data) {
    viewModel.currentList(data.conversationlist);
   }
  });  
 }.bind(viewModel));

 viewModel.unreadCount = ko.dependentObservable(function() {
  var unreadCount = 0;
  for (var i = 0; i < viewModel.currentList().length; i++)
   if (viewModel.currentList()[i].read == true) {
    unreadCount++;
   }
  return unreadCount;
 });

 ko.applyBindings(viewModel);

The above appears to be working, though I’m not sure if I built this correctly. What I want to learn how to do is when you change a checkbox, how to have the unreadCount be automatically updated to reflect the change. I thought that using ko would provide this automatically, but maybe I need to do something in the template’s checkbox data-bind?

Also, once I can modify the checkbox and have that automatically update the View Model & Unread count, what is the right way to then post that update back to the server (Rails)?

Here’s an example JSON response from server:

{
    "conversationlist": [{
        "project_name": "Proj 1",
        "preview": "xxxxx",
        "status": "unread",
        "participants": [{
            "image": "XXXXXX"
        }, {
            "image": "XXXXXX"
        }],
        "conversation_id": 193,
        "title": "Hi Ho",
        "time_ago": "1 day",
        "read": true
    }, {
        "project_name": "Proj 2",
        "preview": "xxxx",
        "status": "unread",
        "participants": [{
            "image": "XXXXXX"
        }, {
            "image": "XXXXXX"
        }],
        "conversation_id": 193,
        "title": "Hi Ho",
        "time_ago": "1 day",
        "read": true
    }, {
        "project_name": "Proj 3",
        "preview": "xxxxx",
        "status": "unread",
        "participants": [{
            "image": "XXXXXX"
        }, {
            "image": "XXXXXX"
        }],
        "conversation_id": 193,
        "title": "Hi Ho",
        "time_ago": "1 day",
        "read": true
    }]
}
  • 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-19T04:05:44+00:00Added an answer on May 19, 2026 at 4:05 am

    Seems like you could execute a function on click (inside data-bind='click: function() {...}') for each item that increments or decrements the unread counter depending on the value of the checkbox that was checked. This way, you wouldn’t ever have to loop through the currentList and update the unread count that way.

    You could also subscribe to the viewmodel’s read property explicitly and execute your own code when read changes (see “Explicitly subscribing to observables” in the middle of the observables documentation).

    Edit: Here’s a thread where one user describes how they set up an observable array with items with observable properties. Here‘s an example that the author (Steve Sanderson) came up with demonstrating an observable arrays with observable properties.

    With both methods, it seems like you could perform an AJAX call to POST back to the server.

    Update: Here’s an example of how you could implement this:

    $(function() {
        var initialData = {/*Data retrieved by AJAX or on page load*/};
    
        var markRead = function(conversation) {
            // Make AJAX POST here to update read status of
            // the conversation
        };
    
        // Convenience object for conversations.
        var Conversation = function(conversation_id, status, read) {
            this.conversation_id = conversation_id;
            this.status = status;
            this.read = ko.observable(read);
        };
    
        var initialUnread = 0;
    
        var viewModel = {
            // Map the conversation list to a new array containing
            // objects with observable properties.
            conversationList: ko.observableArray(ko.utils.arrayMap(
            initialData.conversationlist, function(data) {
                if (!data.read === false) {
                    initialUnread++;
                }
                return new Conversation(
                    data.conversation_id,
                    data.status,
                    data.read);
            })),
            unreadCount: ko.observable(initialUnread),
    
            // Executed when the user toggles a conversation's
            // read status.
            toggleRead: function(conversation) {
                // Update the unread count.
                viewModel.unreadCount(
                    viewModel.unreadCount() + 
                    (conversation.read() ? 1 : -1));
    
                // Update the conversation via AJAX
                markRead(conversation);
                return true;
            }
        };
        ko.applyBindings(viewModel);
    });
    

    Demo here.

    Notes:

    • Doesn’t use the mapping plugin. The general logic should be the same though.
    • If the AJAX call fails, you should probably update the checkbox with its status before the click.
    • You can update the unreadCount property in the viewModel anywhere by calling viewModel.unreadCount(<value>).
    • This is based on several of the examples, specifically this one. The complex examples are particularly eye-opening and demonstrate some cool stuff you can do with KO.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following: <td style=width: 500px;> <div style=width: 400px;>SomeContent</div> </td> For some reason,
Please consider the following HTML: <input type='text' id='name'/> <option id='option'> <select value='1'>hi</select> <select value='2'>bye</select>
Please consider the following: DECLARE @xml XML SET @xml = '<Capture> <Data><DataType>Card Number</DataType><Value>1234567898765</Value></Data> <Data><DataType>Expiry
Please consider the following scenario, There are two web applications App1 & App2. A
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Please consider the following scenario: map(T,S*) & GetMap(); //Forward decleration map(T, S*) T2pS =
I have an app that's about presenting fictional simplified cities. Please consider the following
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
please consider the following scenario for .net 2.0: I have an event that is
Please consider the following code. enum type {CONS, ATOM, FUNC, LAMBDA}; typedef struct{ enum

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.