With Knockout 2.0 using this data-bind:
data-bind="click: $root.deleteSomeEntity.bind($data, $parent)"
in the Knockout viewmodel JavaScript the first argument in
self.deleteSomeEntity = function (data, parent) {
// perform deletion
}
seems to be the parent rather than the data.
Is there a reason for this behavior or something I’m missing?
When you call
bindthe first parameter will be the value ofthis. So, in your callthiswill be$dataand the first argument will be$parent.If
$rootis$parentin this case, then you can just do:$root.deleteSomeEntity.bind($root)KO will pass the data as the first parameter and
thiswill be set to$root.If
$parentis not$root(and you likely don’t want to rely onthisbeing a different object that$rootin your method on root), then you would do something like:$root.deleteSomeEntity.bind($root, $data, $parent)Otherwise, there are certainly ways to make sure that you have the proper
thiswithin your view model. It depends on your structure though.