knockout js custom binding called after internal dom elements rendered

柔情痞子 提交于 2019-12-22 09:58:41

问题


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.

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.

Any help would be much appreciated.


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/17492432/knockout-js-custom-binding-called-after-internal-dom-elements-rendered

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