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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:43:30+00:00 2026-06-18T23:43:30+00:00

I am having trouble binding to elements created by a Knockout template triggered by

  • 0

I am having trouble binding to elements created by a Knockout template triggered by an AJAX call in the afterRender event. As you can see in this Fiddle the results render properly but in the afterRender event handler the elements still aren’t available.

Questions…

  • How is the template even rendering properly since it doesn’t appear to have rendered when the afterRender event handler runs?

  • Why does the AJAX call affect the outcome? If you uncomment out the line above the AJAX call, the template elements are available in the afterRender event.

Here is the code…

HTML

<div id="plugin" data-bind="template: { name: 'fooTemplate', data: $data, afterRender: postProcess }"></div>
<br />
<br />
<hr />
<div id="results"></div>

<script type="text/html" id="fooTemplate">
    <div data-bind="foreach: items()">
        <span data-bind="text: displayText"></span>
    </div>
</script>

JAVASCRIPT

var data = {
    items: [{
        displayText: 'Name',
    }, {
        displayText: 'Last Login Date',
    }, {
        displayText: 'Email',
    }]
};

function DataModel() {
    var self = this;
    self.items = ko.observableArray([]);
    self.data = ko.dependentObservable(function () {
        //self.items(data.items);//   <<== UNCOMMENT THIS LINE AND THE postProcess FUNCTION SHOWS FULLY RENDERED DOM
        $.ajax({
            url: '/some/path',
            error: function () {
                self.items(data.items);
            }
        });
    });

    postProcess = function () {
        $('#results').text($('#plugin').html());
    }
}

dataModel = new DataModel();
ko.applyBindings(dataModel);

One note…the fiddle makes an AJAX call to a bogus address. This was so the Fiddle doesn’t have a server dependency. The error property is leveraged to change the data which causes the template to run. However, in my development environment calling a valid url yields the same outcome as displayed in the fiddle. Also, I am not using data returned from a server to load the template. Rather the fiddle uses a statically defined data source at the top (again just to demo the problem).

  • 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-18T23:43:31+00:00Added an answer on June 18, 2026 at 11:43 pm

    Looking at your example, you’re running into an async issue.

    Here’s how the flow works without the $.ajax call:

    1. The dependentObservable fires to update the value
    2. The self.items gets updated with new values
    3. The template renders
    4. The postProcess method is called and everything looks fine.

    But with the $.ajax call, the done (and error) methods are callbacks, so the flow changes:

    1. The dependentObservable (use ko.computed instead!) fires to update the value
    2. The $.ajax call is made to the server.
    3. The done (or error) callback is queued up for when it’s complete.
    4. The template renders
    5. The postProcess method is called yet self.items is still empty.
    6. The done callback fires now that the server responded and updates self.items
    7. The rendered template is still bound to your viewmodel, so the rendered template is updated.

    I think the easiest fix is to pull your ajax call out of your dependentObservable to allow the situation you have commented out.

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

Sidebar

Related Questions

Using jQuery 1.7 I'm having trouble binding a Click event to some dynamically loaded
I am having trouble binding mouseover mouseon events. I can use off to unbind
I am having trouble with this binding: <sdk:DataGridCheckBoxColumn Header=Invoiced Binding={Binding SalesInvoiceId.HasValue} /> The error
Hihi, Having trouble model binding with this class (the following concrete, not the abstract).
I'm having trouble getting an event binding to work with python/tkinter. I'm simply trying
I'm having trouble binding values to dynamically created controls. When a user loads a
I'm having trouble creating computed values in Breeze and binding to them with Knockout.
I am having trouble binding a Command that is generated up the UI tree
I'm using Hibernate 4.0.1.Final. I'm having trouble binding a session when testing my service.
I'm having some trouble getting binding to work with JQuery. <iframe id=wufooFormz7x3k1 height=281 allowtransparency=true

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.