内部dom元素呈现后调用的敲除js自定义绑定 [英] knockout js custom binding called after internal dom elements rendered

查看:43
本文介绍了内部dom元素呈现后调用的敲除js自定义绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否会以完全错误的方式进行操作!但是,我希望能够在元素中的所有代码执行完之后在元素上调用自定义绑定.

I am not sure if i am going about this in entirely the wrong way! But I would like to be able to call a custom binding on an element after all code within it has been executed.

我尝试了多种方法:模板以及if与afterrender的绑定,以及自定义绑定,但是由于该元素内的内容都使用foreach的事实,因此我尝试调用的所有绑定都在调用之前调用了我的方法foreach将要渲染的dom元素已运行.

I have tried a number of ways: template and if bindings with afterrender, and a custom binding, but due to the fact that the content within the element uses foreach the all the bindings that i have tried call call my method before the dom element that the foreach will render has run.

我唯一想到的选择是延迟我的方法调用几分之一秒,但这似乎有点不客气.

the only option i can think of is to delay the my methods call for a fraction of a second, but this seems a little hacky.

任何帮助将不胜感激.

推荐答案

我不知道您的确切情况,但是除了延迟( setTimeout )之外,还有几种方法可以解决该问题.您的代码.

I don't know your exact scenario, but there are a couple of ways that you could approach it besides delaying (setTimeout) your code.

一种选择是在自定义绑定中使用 ko.applyBindingsToDescendants(context,element).这将强制运行此元素的子代上的所有绑定.然后,您可以继续执行您要运行的代码.您可能希望将自定义绑定放在具有 foreach 的元素的容器上.

One option is to use ko.applyBindingsToDescendants(context, element) in your custom binding. This would force all of the bindings on children of this element to be run. Then, you could proceed with the code that you want to run. You would likely want to put your custom binding on a container of the element that has your foreach.

如果要处理相同的元素,另一种选择是让您的自定义绑定也处理 foreach .在这种情况下,您可以在元素上调用 ko.applyBindingsToNode(element,{foreach:someItems},context),然后继续执行代码.

Another option would be to have your custom binding also handle the foreach, if you are dealing with the same element. In this case, you could call ko.applyBindingsToNode(element, { foreach: someItems }, context) on your element and then proceed with your code.

这篇关于内部dom元素呈现后调用的敲除js自定义绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆