afterRender
works with template bindings, but after converting my templates to components, there does not seem to be any way to use afterRender
. I have tried looking for an example of a component that uses afterRender
, but cannot find anything.
相关问题
- implementing html5 drag and drop photos with knock
- knockout checked binding doesn't update
- Knockout JS - Binding to array of observable Ints
- Knockout.js autocomplete bindingHandler [closed]
- Javascript in Edge only works with devtools open
相关文章
- Handle IE 9 & 10's clear button with Knockout
- jQuery Chosen doesn't update select options wh
- KnockoutJS property doesn't update when changi
- knockout.js - data-bind text default value
- Setting default values for computed Observable Kno
- Mapping: foreach binding work only the first time
- Knockout radio button binding with boolean
- Ajax Post and Redirect with Model Value MVC4
The secret here is http://knockoutjs.com/documentation/custom-bindings.html
so in my component template I do something like
and on the component viewModel I just implement init and/or update, for example:
The Knockout documentation could be improved by pointing out this very useful case. Also, this is such a useful binding, there should be a standard bindings for 'init' and 'update', for example
I could not get the method working as per the above post. However i found a workaround on the git issue list and it doesn't require a custom KO Binding.
Add the below line in your component template html or string of code.
Then create a init function in your module / viewModel:
or depending on your viewModel structure:
Works like a charm. Example of it working is here
http://jsfiddle.net/gLcfxkv6/1/
Thread on knockout git here: https://github.com/knockout/knockout/issues/1533
Thanks to vamps on git for the workaround.
We needed to access DOM elements in a component after switching between different components. We would have liked to use the non-existing "afterRender" binding on components.
We solved it with a Javascript setTimeout, letting KO do its rendering first, and in effect queueing our code after that.
HTML:
The code switching the component: