将参数传递给knockoutjs视图模型中的函数
有这样的锚标记
<a href="#" class="btn btn-success order-btn" data-bind="attr:{'data-tiername':$data.tierName, 'data-identifier' : $parent.identifier}, click: $root.setPath.bind($data,$data.tierName, $parent.identifier)">Send values</a>
我在viewmodel中
var appViewModel = {
setPath: function (data, tier, identifier) {
alert(data);
alert(tier);
alert(identifier);
},
...........
...........
}
结果是一些knockoutjs核心代码显示在警报消息中(可能是observable(),dependentObservable()函数的定义和[Object object],当用JSON.stringify发出警报时它是空的)
为什么这个工作?
data-bind="attr:{'data-tiername':$data.tierName, 'data-identifier' : $parent.identifier}
但不是这个:
click: $root.setPath.bind($data,$data.tierName, $parent.identifier)
请注意,tierName 是一个 observable(),标识符是compute()
我在哪里可以找到有关 bind() 的更多信息?
I have anchor tag like this
<a href="#" class="btn btn-success order-btn" data-bind="attr:{'data-tiername':$data.tierName, 'data-identifier' : $parent.identifier}, click: $root.setPath.bind($data,$data.tierName, $parent.identifier)">Send values</a>
In the viewmodel
var appViewModel = {
setPath: function (data, tier, identifier) {
alert(data);
alert(tier);
alert(identifier);
},
...........
...........
}
The result is some knockoutjs core code being displayed in alert message (possibly definitions of observable(),dependentObservable() functions and [Object object] which is empty when alerted with JSON.stringify)
why does this work?
data-bind="attr:{'data-tiername':$data.tierName, 'data-identifier' : $parent.identifier}
but not this:
click: $root.setPath.bind($data,$data.tierName, $parent.identifier)
note that tierName is an observable(), identifier is computed()
Where can I find more about bind() ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
tierName
和identifier
是可观察量,因此您需要调用它们来访问它们的值:此外,
bind()
中的第一个参数将被绑定到setPath
中的this
,所以我猜你需要这样的东西:最后,如果
$data
本身是一个可观察的(不清楚如果它来自您的代码),那么您需要将其称为好吧:还请记住 bind 已在 ECMAScript 5 中引入,因此它可能不会出现在所有浏览器中。所以我可能会这样做:
Here 是额外的关于绑定的信息。
Since
tierName
andidentifier
are observables, you need to call them to access to their values:Also, first argument in
bind()
will be bound tothis
insetPath
, so I guess you need something like this:Finally, if
$data
itself is an observable (which is not clear if it is from you code), then you need to call it as well:Also remember bind has been introduced in ECMAScript 5, so it may not be present in all browsers. So I would probably do something like this instead:
Here is additional info on bind.
bind
的第一个参数是执行函数时的目标(您希望this
成为的目标)。因此,如果您希望data
成为第一个参数,那么它就必须是第二个参数。在函数内部,如果您正在处理可观察值或计算可观察值,那么您需要解开它们以查看它们的值。您可以通过将其作为函数
alert(data());
调用或调用alert(ko.utils.unwrapObservable(data));
(即通常当您在设计类型中不知道要处理的是可观察的还是不可观察的时使用,这是当您传递可观察/时
attr
和其他绑定起作用的原因。计算的observable 的一点是,为了方便起见,它们都调用了 ko.utils.unwrapObservable(因此在传递 observables 时不必添加()
,除非您正在编写表达式 < code>!$data()。这是一些关于绑定的参考:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
The first parameter to
bind
is the target (what you wantthis
to be) when your function is executed. So, if you wantdata
to be the first argument, then it needs to be the second argument.Inside your function, if you are dealing with observables or computed observables, then you need to unwrap them to see their value. You would do this either by calling it as a function
alert(data());
or by callingalert(ko.utils.unwrapObservable(data));
(which is generally used when you don't know at design type if what you are going to be dealing with is an observable or a non-observable.The reason that
attr
and other bindings work when you pass an observable/computed observable is that they all callko.utils.unwrapObservable
for you for convenience (so you don't have to add()
when passing observables unless you are writing an expression!$data()
.Here is some reference on bind: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind