为什么我无法在此自定义绑定中获取当前的 viewModel?
这是包含我的绑定的 .haml 代码。我删除了不相关的绑定。
#date-extension
.filter-extension-container
.filter-extension-button
.button-close
#hand-graph-container{"data-bind" => "with:dateGraph"}
#x-axis
#hand-graph{"data-bind" => "foreach: {data:graphData}"}
%div{"data-bind" => "interactiveBar: $data"}
我开始了自定义绑定,就像这样。
ko.bindingHandlers.interactiveBar = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
debugger;
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
}
};
但是当我查看 viewModel 时,它等于 valueAccessor 并且只是我传入的数据对象。我想访问 dateGraph viewModel,并且应该能够访问它,因为我已经使用了“with: graphData”根据文档。
This is the .haml code that contains my bindings. I removed bindings that weren't relevant.
#date-extension
.filter-extension-container
.filter-extension-button
.button-close
#hand-graph-container{"data-bind" => "with:dateGraph"}
#x-axis
#hand-graph{"data-bind" => "foreach: {data:graphData}"}
%div{"data-bind" => "interactiveBar: $data"}
And I have the beginnings of a custom binding, like so.
ko.bindingHandlers.interactiveBar = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
debugger;
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
}
};
But when I look at viewModel, it is equal to the valueAccessor and is just the data object that I'm passing in. I'd like access to the dateGraph viewModel, and should be able to access it since I have used "with: graphData" according to the documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
foreach
内部,viewModel
属性是在该范围级别绑定的数据。有几个选项(假设您使用的是 Knockout 2.0):
传递
$parent
而不是$data
并访问您的dateGraph
对象通过valueAccessor()
或者绑定处理程序的第五个参数实际上是绑定上下文。绑定上下文将具有
$data
、$parent
、$parents
和$root
属性。您可以在此处查看属性的说明。Inside of a
foreach
theviewModel
property is the data being bound at that scope level.There are a couple of options (assuming you are using Knockout 2.0):
pass
$parent
instead of$data
and access yourdateGraph
object viavalueAccessor()
or the 5th argument to a binding handler is actually the binding context. The binding context will have
$data
,$parent
,$parents
, and$root
properties. You can see a description of the properties here.