Consider the following example:
HTML:
<div data-bind="with: job">
<div data-bind="foreach: tasks">
<div>
<span data-bind="text: $data"></span>
<span data-bind="click: $parent.allDone">(Done)</span>
</div>
</div>
</div>
JS:
ko.applyBindings(function() {
this.job = {
tasks: ['Buy milk', 'Sleep', 'Work', 'Praise'],
allDone: function(data) {
console.log(this);
console.log(data);
}
};
});
Could you clarify the following points:
- Why the value of
thisinallDoneis notjob? Isn'tallDonecalled via$parentwho is thejob? - Is there a way to call
allDoneon click such that the value ofthiswould bejob? The value of
thisanddataseems similar, but not quite the same:this -> String {0: "S", 1: "l", 2: "e", 3: "e", 4: "p", length: 5}data -> SleepWhat is the difference?