Throttling with knockout mapping plug

懵懂的女人 提交于 2019-12-06 02:09:25

IE is a naughty little.....isn't it.

When doing data binding with javascript if there is going to be a significant UI update I do something like the following.

function ajaxCallback(listOfDataItems){

  var addToUiFunction = function(item){
     // add it to the ko.observable array which is data bound to the UI
     myDataBoundArray.push(item);
  };

  for (var i = 0, arrayLength = listOfDataItems.length; i < arrayLength; i++){
    var temp = listOfDataItems[i];

    //create an immediately executing function to close around
    //the item that returns a function to call at a later date.
    var tempFunction = (function(){
      var item = temp;
      return function() { addToUiFunction(item) };
    })();

    setTimeout(tempFunction, 0);

  }
}

What has happened here is that I only add one item to the UI at once. I use setTimeout with a delay of 0 to defer the execution of an individual add until the current call finishes. This means very short units of work that won't time out your browser.

p.s. the code is a little dodgy, it's just trying to illustrate a point.

We had the same problem too. Our viewmodel had one too many computed observables and it was causing the script to run more slowly. Removing unnecessary subscribles might save you from this problem.

I know this isn't an ideal answer, but if your situation allows it, you can always just not map the inner items. Ex. If your ajax call is returning 1000 people and you want your UI to update and show all of those people, you can have your viewmodel have an observableArray of raw people js objects (and not their ko mapped equivalent). If you add or remove items from your observableArray, it'll show correctly on the UI, but it will not help you if need to subscribe to all of the property change events on all of each person's properties.

Typically when I'm pulling this many items, it is for reporting and so I don't need to edit the items themselves, but do need to add/remove rows from the report depending on filter criteria.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!