I am passing $index
and $data
to the change_model
function. The function is expecting 2 parameters in the following order: (index, data)
.
From the viewModel I am passing click: $root.change_model.bind($data, $index())
. Within the function index
prints $data
, and data
prints index
: values are reversed.
self.change_model = function(index, data) {
self.patternSelectedIndex(index);
selected_door = data.file;
create_door();
};
<div data-bind="foreach: x.patterns">
<div class="thumbnail" data-bind="css: { selected: $index() === $root.patternSelectedIndex() }">
<img class='img model' style='width:164px;height:90px;padding:5px' data-bind="attr:{src:'images/models/' + $data.file + '.png'}, click: $root.change_model.bind($data, $index())" />
<div class="caption">
<span data-bind="text: $data.name"></span>
</div>
</div>
</div>
The first argument of
bind
will becomethis
inside your function, because Knockout is merely using the regularbind
function.You can either pass
$data
or$root
as the first (thisArg
) argument, or pass null or undefined, as you don't really need it since you seem to use theself = this
idiom.For example: