javascript - knockout.js referring to parent object - Stack Overflow

I have a plex knockout.js object which is effectively an observableArray with another observableArray i

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
Add a ment  | 

1 Answer 1

Reset to default 7

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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961547a4603415.html

相关推荐

  • javascript - knockout.js referring to parent object - Stack Overflow

    I have a plex knockout.js object which is effectively an observableArray with another observableArray i

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信