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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:58:24+00:00 2026-05-31T03:58:24+00:00

I have a list of attachments on a page which is generated using a

  • 0

I have a list of attachments on a page which is generated using a jQuery $.ajax call and Knockout JS.

My HTML looks like (this is stripped back):

<tbody data-bind="foreach: attachments">
  <tr>
    <td data-bind="text: Filename" />
  </tr>
</tbody>

I have a function that gets the list of attachments which is returned as a JSON response:

$(function () {
  getFormAttachments();
});

function getAttachments() {
  var request = $.ajax({
    type: "GET",
    datatype: "json",
    url: "/Attachment/GetAttachments"
  });

  request.done(function (response) {
    ko.applyBindings(new vm(response));
  });
}

My view model looks like:

function vm(response) {
  this.attachments = ko.observableArray(response);
};

There is a refresh button that the use can click to refresh this list because over time attachments may have been added/removed:

$(function () {
  $("#refresh").on("click", getAttachments);
});

The initial rendering of the list of attachments is fine, however when I call getAttachments again via the refresh button click the list is added to (in fact each item is duplicated several times).

I’ve created a jsFiddle to demonstrate this problem here:

http://jsfiddle.net/CpdbJ/137

What am I doing wrong?

  • 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-31T03:58:26+00:00Added an answer on May 31, 2026 at 3:58 am

    Here is a fiddle that fixes your sample. Your biggest issue was that you were calling ‘applyBindings’ multiple times. In general you will call applyBindings on page load and then the page will interact with the View Model to cause Knockout to refresh portions of your page.

    http://jsfiddle.net/CpdbJ/136

    html

    <table>
        <thead>
            <tr><th>File Name</th></tr>
        </thead>
        <tbody data-bind="foreach: attachments">
          <tr><td data-bind="text: Filename" /></tr>
        </tbody>
    </table>
    <button data-bind="click: refresh">Refresh</button>
    

    javascript

    $(function () {
      var ViewModel = function() {
        var self = this;
    
        self.count = 0;
        self.getAttachments = function() {
          var data = [{ Filename: "f"+(self.count*2+1)+".doc" },
                      { Filename: "f"+(self.count*2+2)+".doc"}];
          self.count = self.count + 1;
          return data;
        }
    
        self.attachments = ko.observableArray(self.getAttachments());
    
        self.refresh = function() {
          self.attachments(self.getAttachments());        
        }
      };
    
      ko.applyBindings(new ViewModel());
    });
    

    —

    You may also want to look at the mapping plugin – http://knockoutjs.com/documentation/plugins-mapping.html. It can help you transform JSON into View Models. Additionally it is able to assign a property to be the “key” for an object… this will be used to determine old vs new objects on subsequent mappings.

    Here is a fiddle I wrote a while back to demonstrate a similar idea:

    http://jsfiddle.net/wgZ59/276

    NOTE: I use ‘update’ as part of my mapping rules, but ONLY so I can log to the console. You would only need to add this if you wanted to customize how the mapping plugin updated objects.

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

Sidebar

Related Questions

I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have a SharePoint list, which has attachments!! I am using a custom grid-view
I have a List defined like this : public List<string> AttachmentURLS; I am adding
I have a SharePoint List to which I'm adding new ListItems using the Client
I have a list of files (stored in db which I get using Linq),
I have List I want to sort Desc by Priority, which is int and
I have list of files which contain particular patterns, but those files have been
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
I have a products page list and i am exporting it to a word
I currently have a task list, some of them contain file attachments. As part

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.