更新时间:2023-11-30 22:19:52
KO通常使用$ data和$ root关键字.
当您在数组上使用foreach时(数据绑定),KO为数组中的每个元素创建一个<li>
.
The $data and $root keywords are typically used by KO.
When you use the foreach on array ( data-bind), KO creates one <li>
for each element in the array.
在这种情况下,$ data是数组的当前项(如folders [i]),而$ root是父元素. 对您来说,文件夹是ViewModel的一个字段: $ data =迭代中的当前文件夹(viewModel.folder [i]) $ root = viewModel
In this case, $data is the current item of array (like folders[i]) and $root is the parent element. For you, folders is a field of your ViewModel : $data = current folders on the iteration (viewModel.folder[i]) $root = viewModel
self.chosendFolderId(folder)执行viewModel selectedFolderId方法.该代码使用self保留viewModel的值,因为在函数中关键字"this"不是viewModel而是方法的发送者.这是一个关闭.
self.chosendFolderId(folder) execute you viewModel chosenFolderId method. The code use self to keep the viewModel value because in the function the keyword "this" isn't the viewModel but the method's sender. It's a closure.
$ parent关键字是树的上一级. $ root关键字是顶层树的级别.
Edit : The $parent key word is the tree's previus level. The $root key word is the top tree's level.
viewModel {
topObjects : ko.observableArray()
}
viewModel.topObjects.push({
Objects : ko.observableArray()
});
如果我们在viewModel.topObjects.Objects上创建foreach,则$ parent是topObjects,$ root是viewModel.
If we create a foreach on viewModel.topObjects.Objects, the $parent is topObjects, $root is viewModel.
感谢Tjorriemorrie;-)
Thanks Tjorriemorrie ;-)