I have a plex 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?
I have a plex 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?
Share Improve this question asked Jan 7, 2013 at 9:03 BartoszBartosz 4,60211 gold badges45 silver badges70 bronze badges 1-
1
You can try to pass the
$parent
as parameter:data-bind="click: function(data, event) { return $root.RemoveActivity(data, event, $parent); }"
then you will get the parent in the thrid parameter. – nemesv Commented Jan 7, 2013 at 9:07
1 Answer
Reset to default 7mhu'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);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961547a4603415.html
评论列表(0条)