3

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.

nodrog
  • 3,532
  • 2
  • 25
  • 31
  • I am not sure what "after all code" has been executed. If you mean "after a binding such as foreach" has executed, then this might help: http://stackoverflow.com/questions/16592237/custom-binding-for-when-foreach-has-finished-rendering/22383861#22383861 – j-a Mar 13 '14 at 15:49

1 Answers1

2

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.

RP Niemeyer
  • 114,592
  • 18
  • 291
  • 211
  • thanks @RP Niemeyer, yes the second option is probably the best. The issue is that i am adding a custom scroll to a div that holds the elements that are not yet rendered, but can only run it once they are rendered. Would not be a problem except that the size of the box that needs scrolling changes size the scroll library needs to be reset. It of a pain. thanks – nodrog Jul 08 '13 at 10:44