te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Refreshing list after ajax call with Knockout JS - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Refreshing list after ajax call with Knockout JS - Stack Overflow

programmeradmin4浏览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:

What am I doing wrong?

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/CpdbJ/137

What am I doing wrong?

Share Improve this question edited Jun 5, 2013 at 9:46 user2022859 asked Mar 4, 2012 at 15:19 KevKev 120k53 gold badges305 silver badges391 bronze badges 3
  • Kev - nice post. I'm still a little confused by it. New to knockout. I am having similar trouble simply loading a ViewModel with AJAX data on page load. I'm sure i'm missing a tiny but serious point. one thing that confuses me is some tutorials show viewmodel as an object (ex. var viewmodel = { something: ko.observable() }) and others as a function (ex. function ViewModel() { this.something = ko.observable() }) - any suggestions? – one.beat.consumer Commented Mar 16, 2012 at 0:43
  • @one.beat.consumer - see my follow-up question: stackoverflow./questions/9589419/… - the answer and the ments below should make things clearer. I'd remend spending $25 and watching this: pluralsight-training/microsoft/Courses/… – Kev Commented Mar 16, 2012 at 2:30
  • I'll check out your other question; thank you. I have a pluralsight subscription and I have watched it 2-3 times... they barely scratch the surface and much of it is old now that 2.0 is out, they were using 1.2 or 1.3 beta... – one.beat.consumer Commented Mar 16, 2012 at 5:48
Add a ment  | 

1 Answer 1

Reset to default 10

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/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./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/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)

  1. 暂无评论