Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockoutJS bind event after template render

I've been searching for a while, and I'm pretty confident this is a new question, and not a repeat like the title suggests. :)

Basically, I'm trying to find out if there is a subscribe-able event that KnockoutJS creates after a template render when using something like jQuery templates.

I'd use the built-in "afterRender" but I found out that it doesn't fire if the observable array is cleared. I built this demo to illustrate that problem: http://jsfiddle.net/farina/YWfV8/1/.

Also, I'm aware that I could write a custom handler...but that seems really unnecessary for what I need.

I just want one event that fires after the template finishes rendering.

like image 630
farina Avatar asked Feb 16 '12 05:02

farina


People also ask

How can you bind data to templates?

Bind properties in a component's template to properties in the component's JavaScript class. In the template, surround the property with curly braces, { property } . To compute a value for the property, use a JavaScript getter in the JavaScript class, get property (){} .

What is $data in Knockout?

The $data variable is a built-in variable used to refer to the current object being bound. In the example this is the one of the elements in the viewModel. folders array.

What is $root in Knockout?

$root. This is the main view model object in the root context, i.e., the topmost parent context. It's usually the object that was passed to ko. applyBindings . It is equivalent to $parents[$parents.

What is applyBindings in Knockout?

applyBindings do, The first parameter says what view model object you want to use with the declarative bindings it activates. Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes. For example, ko.


1 Answers

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.

  • http://jsfiddle.net/farina/kuFx2/1/ (Using object array style ViewModel)

  • http://jsfiddle.net/farina/QtZm2/1/ (Using function style ViewModel)

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

Thanks for the help Dan!!

like image 173
farina Avatar answered Sep 18 '22 15:09

farina