I have anchor tag like this
<a href="#" class="btn btn-success order-btn" data-bind="attr:{'data-tiername':$data.tierName, 'data-identifier' : $parent.identifier}, click: $root.setPath.bind($data,$data.tierName, $parent.identifier)">Send values</a>
In the viewmodel
var appViewModel = {
setPath: function (data, tier, identifier) {
alert(data);
alert(tier);
alert(identifier);
},
...........
...........
}
The result is some knockoutjs core code being displayed in alert message (possibly definitions of observable(),dependentObservable() functions and [Object object] which is empty when alerted with JSON.stringify)
why does this work?
data-bind="attr:{'data-tiername':$data.tierName, 'data-identifier' : $parent.identifier}
but not this:
click: $root.setPath.bind($data,$data.tierName, $parent.identifier)
note that tierName is an observable(), identifier is computed()
Where can I find more about bind() ??
Since
tierNameandidentifierare observables, you need to call them to access to their values:Also, first argument in
bind()will be bound tothisinsetPath, so I guess you need something like this:Finally, if
$dataitself is an observable (which is not clear if it is from you code), then you need to call it as well:Also remember bind has been introduced in ECMAScript 5, so it may not be present in all browsers. So I would probably do something like this instead:
Here is additional info on bind.