knockout js custom binding called after internal d

2019-05-11 23:17发布

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.

标签: knockout.js
1条回答
Lonely孤独者°
2楼-- · 2019-05-12 00:05

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.

查看更多
登录 后发表回答