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
this
inallDone
is notjob
? Isn'tallDone
called via$parent
who is thejob
? - Is there a way to call
allDone
on click such that the value ofthis
would bejob
? The value of
this
anddata
seems similar, but not quite the same:this -> String {0: "S", 1: "l", 2: "e", 3: "e", 4: "p", length: 5}
data -> Sleep
What is the difference?