knockout.js完成呈现所有元素后的成功回调

yqyhoc1h  于 2022-11-10  发布在  其他
关注(0)|答案(8)|浏览(187)

我已经实现了一个knockout foreach绑定,在同一个页面中有多个模板,这里给出了一个例子,我感兴趣的是找出一个块何时完成渲染,我已经尝试了afterRenderafterAdd,但我猜它会为每个元素运行,而不是在整个循环完成后运行。

<ul data-bind="foreach: {data: Contacts, afterAdd: myPostProcessingLogic}">
  <li>
    <div class="list_container gray_bg mrgT3px">
      <div class="list_contact_icon"></div>
      <div class="contact_name"><span data-bind="text: first_name"></span> <span data-bind="text: last_name"></span></div>
      <div class="contact_number"><span data-bind="text: value"></span></div>
      <div class="callsms_container">
        <a href="#notification-box" class="notifcation-window">
          <div class="hover_btn tooltip_call">
            <div class="hover_call_icon"></div>
            <span>Call</span></div>
        </a>
        <a class="sendsms" href="#sendsms" rel="#sendsms">
          <div class="hover_btn tooltip_sms">
            <div class="hover_sms_icon"></div>
            <span>SMS</span></div>
        </a>
        <a href="#">
          <div class="hover_more_btn"></div>
        </a>
      </div>
      <!-- close callsms container -->
      <div id="notification-box" class="notification-popup">
        <a href="#" class="close"><img class="btn_close" src="images/box_cross.png" /></a> <img class="centeralign" src="images/notification_call.png" /> <span>Calling... +44 7401 287366</span> </div>
      <!-- close notification box -->
      <!-- close list gray bg -->
      <div class="tooltip_description" style="display:none" id="disp"> asdsadaasdsad </div>
    </div>
  </li>
</ul>

我感兴趣的是找出循环完成渲染时的成功回调。
下面是我的afterAdd函数,它基本上附加了一些jQuery事件,没有什么特别的。

myPostProcessingLogic = function(elements) { 
  $(function(){
      $(".list_container_callog").hover(function(){  
          $(".callsms_container", this).stop().animate({left:"0px"},{queue:false,duration:800});
      }, function() {
          $(".callsms_container", this).stop().animate({left:"-98%"},{queue:false,duration:800});
      });
  });
}

提前感谢,并告诉我有一个成功的回调:)

bejyjqdl

bejyjqdl1#

knockout.js中有afterRender回调:

foreach: { data: myItems, afterRender: renderedHandler }

Here's documentation.
在处理程序中检查呈现集合的长度是否等于items集合的长度,如果不相等,就不要执行要使用的完整呈现逻辑。

renderedHandler: function (elements, data) {
    if ($('#containerId').children().length === this.myItems().length) {
        // Only now execute handler
    }
}
8ehkhllq

8ehkhllq2#

尝试将ul Package 为

<div data-bind='template: {afterRender: myPostProcessingLogic }'>

它只在第一次呈现模板中的所有内容时才起作用。但是您只会得到一次对myPostProcessingLogic的调用。

<div data-bind='template: {afterRender: myPostProcessingLogic }'>
  <ul data-bind="foreach: Contacts">
    <li>
      <div class="list_container gray_bg mrgT3px">
        <div class="list_contact_icon"></div>
        <div class="contact_name"><span data-bind="text: first_name"></span> <span data-bind="text: last_name"></span></div>
        <div class="contact_number"><span data-bind="text: value"></span></div>
        <div class="callsms_container">
          <a href="#notification-box" class="notifcation-window">
            <div class="hover_btn tooltip_call">
              <div class="hover_call_icon"></div>
              <span>Call</span></div>
          </a>
          <a class="sendsms" href="#sendsms" rel="#sendsms">
            <div class="hover_btn tooltip_sms">
              <div class="hover_sms_icon"></div>
              <span>SMS</span></div>
          </a>
          <a href="#">
            <div class="hover_more_btn"></div>
          </a>
        </div>
        <!-- close callsms container -->
        <div id="notification-box" class="notification-popup">
          <a href="#" class="close"><img class="btn_close" src="images/box_cross.png" /></a> <img class="centeralign" src="images/notification_call.png" /> <span>Calling... +44 7401 287366</span> </div>
        <!-- close notification box -->
        <!-- close list gray bg -->
        <div class="tooltip_description" style="display:none" id="disp"> asdsadaasdsad </div>
      </div>
    </li>
  </ul>
</div>
xtupzzrd

xtupzzrd3#

上面Chuck Schneider的回答是最好的。我不得不使用无容器控制,因为foreach在tbody元素上:

<!-- ko template: {afterRender: SetupCheckboxes } -->
<tbody data-bind="foreach: selectedItems" id="gridBody">
  <tr>
    <td>
      <input type="checkbox" />
    </td>
  </tr>
</tbody>
<!-- /ko -->
uklbhaso

uklbhaso4#

只需使用Knockout的无容器方法将foreach封装到另一个foreach循环中,如下所示:

<!-- ko foreach:{data: Contacts, afterRender: myPostProcessingLogic }-->
<ul data-bind="foreach: $data}">
  <li>
    <div class="list_container gray_bg mrgT3px">
      <div class="list_contact_icon"></div>
      <div class="contact_name"><span data-bind="text: first_name"></span> <span data-bind="text: last_name"></span></div>
      <div class="contact_number"><span data-bind="text: value"></span></div>
      <div class="callsms_container">
        <a href="#notification-box" class="notifcation-window">
          <div class="hover_btn tooltip_call">
            <div class="hover_call_icon"></div>
            <span>Call</span></div>
        </a>
        <a class="sendsms" href="#sendsms" rel="#sendsms">
          <div class="hover_btn tooltip_sms">
            <div class="hover_sms_icon"></div>
            <span>SMS</span></div>
        </a>
        <a href="#">
          <div class="hover_more_btn"></div>
        </a>
      </div>
      <!-- close callsms container -->
      <div id="notification-box" class="notification-popup">
        <a href="#" class="close"><img class="btn_close" src="images/box_cross.png" /></a> <img class="centeralign" src="images/notification_call.png" /> <span>Calling... +44 7401 287366</span> </div>
      <!-- close notification box -->
      <!-- close list gray bg -->
      <div class="tooltip_description" style="display:none" id="disp"> asdsadaasdsad </div>
    </div>
  </li>
</ul>
<!-- /ko -->
qlvxas9a

qlvxas9a5#

上面的解决方案效果很好。另外,如果你需要使用foreach的“as”选项,你可以这样做:

data-bind="foreach: { data: myItems, afterRender: renderedHandlet, as: 'myItem'}">
jk9hmnmh

jk9hmnmh6#

在3.5版中,Knockout提供了绑定节点内容时要通知的事件
HTML语言

<div data-bind="childrenComplete: bindingComplete">...</div>

JavaScript语言

function bindingComplete(){
...
}

如果您在封装了所有子数据绑定表达式的DOM中的某个点创建事件绑定表达式,那么它就相当于一个页面绑定完成事件
参考https://knockoutjs.com/documentation/binding-lifecycle-events.html

esyap4oy

esyap4oy7#

我最近刚刚向knockout发出了一个拉取请求,要求他们添加两个事件,以便在绑定、解包然后在渲染项目之前和渲染完所有项目之后调用正确的位置。我还没有收到他们的任何回复,但这确实是你想做的,但你不必编写黑客代码来让它工作。我很惊讶,以前没有人提出过这个要求。我使用了这些回调函数,我添加到源代码中来销毁并重新初始化一个knockout绑定的jquery数据表。这是最简单的解决方案。我在网上看到过很多尝试,但这是最简单的解决方案。
提取请求:--〉pr 1856

ko.bindingHandlers.DataTablesForEach = {

  init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
    var nodes = Array.prototype.slice.call(element.childNodes, 0);
    ko.utils.arrayForEach(nodes, function(node) {
      if (node && node.nodeType !== 1) {
        node.parentNode.removeChild(node);
      }
    });
    return ko.bindingHandlers.foreach.init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
  },
  update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {

    var value = ko.unwrap(valueAccessor()),
      key = "DataTablesForEach_Initialized";

    var newValue = function() {
      return {
        data: value.data || value,
        beforeRenderAll: function(el, index, data) {

          if (ko.utils.domData.get(element, key)) {

            $(element).closest('table').DataTable().destroy();
          }
        },
        afterRenderAll: function(el, index, data) {
          $(element).closest('table').DataTable(value.options);
        }
      };
    };

    ko.bindingHandlers.foreach.update(element, newValue, allBindingsAccessor, viewModel, bindingContext);

    //if we have not previously marked this as initialized and there is currently items in the array, then cache on the element that it has been initialized
    if (!ko.utils.domData.get(element, key) && (value.data || value.length)) {
      ko.utils.domData.set(element, key, true);
    }

    return {
      controlsDescendantBindings: true
    };
  }
};

Knockout Datatables JSFiddle

cpjpxq1n

cpjpxq1n8#

尝试knockout.js中的afterRenderAll回调:
foreach:{数据:我的项目,全部渲染后:我的后期处理逻辑}

相关问题