Reputation: 4592
I have a complex knockout.js object which is effectively an observableArray with another observableArray inside. I have a remove function which asynchronously removes chosen element from the second array. An item is being archived in the database, while one of its observable properties on client side is being set to false making it to disappear from the screen.
A remove button event is created using $root keyword:
<a href="#" data-bind="click: $root.RemoveActivity">Remove</a>
What gives me access to details of the chosen element using "this" keyword. My problem is, that while deleting item from the second array I would like to change something to its parent item in the first array. As I mentioned "this" keyword refers to the child item, is there any way I could access the parent item at the same time as well?
Upvotes: 3
Views: 3895
Reputation: 17554
mhu's answer is a antipattern because it creates a dependency between ViewModel and the structure of the View.
Instead do
<a href="#" data-bind="click: $parent.removeActivity.bind($parent)">Remove</a>
Parent Viewmodel
removeActivity: function(activity) {
this.activities.remove(activity);
}
Upvotes: 7