KnockoutJS bind event after template render

时光毁灭记忆、已成空白 提交于 2019-11-30 11:14:04

My colleague actually solved this last night using something we were playing with before I went home.

So the whole "problem" with the events “afterRender”, “afterAdd”, and “beforeRemove” is that they act differently in conjunction with a "foreach" binding. KnockoutJS is nice enough to tell you this on their page, but for whatever reason it didn't actually sink in for me until I saw it in practice.

What really works is to scrap the whole "foreach" binding and use Knockout's native "data" bind like this:

data-bind="template: { name: 'item-template', data: items, afterRender: caller }"

Then "afterRender" works exactly as the name suggests.

I was under the impression that you couldn't iterate the collection and render new UI without foreach, but these examples illustrate that it does work.

I made an example for both ViewModel styles because I sometimes need one or the other.

Thanks for the help Dan!!

Is beforeRemove is what you are looking for? I am not sure what behaviour you want to achieve. Please checkout this sample: http://jsfiddle.net/romanych/YWfV8/8/ Is it what you want or not?

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