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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:53:31+00:00 2026-06-09T06:53:31+00:00

So this question follows on from my previous question Under what conditions does a

  • 0

So this question follows on from my previous question Under what conditions does a Knockout native template re-render

After some investigation, it seems that if a template defines an afterRender function and that afterRender function references a part model then, if that part of the model is subsequently altered, the afterRender function is called again.

This behaviour seems similar to computedObservables (aka dependentObservables) where knockout tracks which parts of the model it depends on and then re-runs the computedObservables whenever one of the values it depends on changes.

To demonstrate this I have written a jsFiddle: http://jsfiddle.net/unklefolk/nczCt/1/

As expected the afterRender function is called when the view is rendered. However, if you click the checkbox, the afterRender function is run again as it uses viewModel.selectedPet().pet().IsHappy()

This does not make sense to me. So my question is:

  • Is this a bug that the afterRender function is being re-run?
  • How can I ensure that the afterRender function is only run once?

Thanks in advance.

  • 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-09T06:53:36+00:00Added an answer on June 9, 2026 at 6:53 am

    Knockout uses a computed observable to wrap the execution of the bindings on an element. This is how the bindings are triggered again when a dependency changes (this link might help explain further.

    Currently, the afterRender code is not excluded from adding to these dependencies. If this is not desirable for your scenario, then there are a couple of ways to prevent it.

    • run your code in a setTimeout: like http://jsfiddle.net/rniemeyer/nczCt/2/
    • wrap your code in its own computed observable that you immediately dispose like: http://jsfiddle.net/rniemeyer/nczCt/3/ (I would do this for now to avoid the setTimeout)
    • In KO 2.2 you will be able to use the new peek function on observables that allows you to get the value without creating a dependency.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question follows on from a previous question, that has raised a further issue.
This question follows on from a previous question. However stackoverflow presents me from commenting
This problem follows on from a previous question . When I run the following
This question follows on from a previous question which can be found here: Need
This follows on from my previous question about Moose structured types. I apologise for
This question follows on from a previous question... How do I create the current
This follows on from a previous question here where I asked how to develop
OK, this kind of follows on from my previous question . What I would
This question follows directly from my previous question here in SO . I think
This is a follow on from my previous question although this is about something

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.