When $parent.foo is a prototype method, the this is not the parent

While working with prototype methods in Knockout, “this” will not be accessible if you bind like:
data-bind=”click: $parent.foo”

Do not pass the function directly to the click binding, wrap it in an anonymous function to have it called in context of your view model:

data-bind=”click: function() { $parent.foo() }”
or
data-bind=”click: $parent.foo.bind($parent)”

this way when the binding is activated it will use $parent as context of the call – i.e. this

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s