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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:30:50+00:00 2026-06-02T05:30:50+00:00

I have a simple observableArray which contains a lot of user-models. In the markup,

  • 0

I have a simple observableArray which contains a lot of user-models.
In the markup, there is a template with a foreach loop which loops the users and outputs them in a simple table. I additionally style the table with a custom scrollbar and some other javascript. So now I have to know when the foreach loop is finished and all the models are added to the DOM.

The problem with the afterRender callback is that it gets called every time something is added, but I need kind of a callback which fires only once.

  • 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-02T05:30:53+00:00Added an answer on June 2, 2026 at 5:30 am

    Your best bet is to use a custom binding. You can either place your custom binding after foreach in the list of bindings in your data-bind or you could execute your code in a setTimeout to allow foreach to generate the content before your code is executed.

    Here is a sample that shows running code a single time and running code each time that your observableArray updates: http://jsfiddle.net/rniemeyer/Ampng/

    HTML:

    <table data-bind="foreach: items, updateTableOnce: true">
        <tr>
            <td data-bind="text: id"></td>
            <td data-bind="text: name"></td>
        </tr>
    </table>
    
    <hr/>
    
    <table data-bind="foreach: items, updateTableEachTimeItChanges: true">
        <tr>
            <td data-bind="text: id"></td>
            <td data-bind="text: name"></td>
        </tr>
    </table>
    
    <button data-bind="click: addItem">Add Item</button>
    

    JS:

    var getRandomColor = function() {
       return 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';  
    };
    
    ko.bindingHandlers.updateTableOnce = {
        init: function(element) {
            $(element).css("color", getRandomColor());            
        }    
    };
    
    //this binding currently takes advantage of the fact that all bindings in a data-bind will be triggered together, so it can use the "foreach" dependencies
    ko.bindingHandlers.updateTableEachTimeItChanges = {
        update: function(element) {    
            $(element).css("color", getRandomColor());  
        }    
    };
    
    
    var viewModel = {
        items: ko.observableArray([
            { id: 1, name: "one" },
            { id: 1, name: "one" },
            { id: 1, name: "one" }
        ]),
        addItem: function() {
            this.items.push({ id: 0, name: "new" });   
        }
    };
    
    ko.applyBindings(viewModel);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been a lot of questions posted about using checkboxes in a foreach
I have simple user registration form. In which i am puting city as a
This should be a simple one: I have an observableArray object called To in
I have simple class with width and height member fields which define number of
Have simple Spring Security webapp with password encoding: <security:authentication-manager alias=authenticationManager> <security:authentication-provider user-service-ref=personService> <security:password-encoder hash=md5
Have simple C# console app which imports text data into SQL. It takes around
I have simple user model: var user = Backbone.Model.extend({ initialize: function(){ this.bind(change:auth, function (){
I have simple models with generic relations from this example at the Django Project
I have simple insert into.. query which is taking around 40 seconds to execute.
I have a simple collection bound to an observableArray that is initially empty. When

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.