将参数传递给knockoutjs视图模型中的函数

发布于 2025-01-08 17:46:51 字数 940 浏览 2 评论 0原文

有这样的锚标记

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剪不断理还乱 2025-01-15 17:46:51

由于 tierNameidentifier 是可观察量,因此您需要调用它们来访问它们的值:

click: $root.setPath.bind($data, $data.tierName(), $parent.identifier())

此外,bind() 中的第一个参数将被绑定到 setPath 中的 this ,所以我猜你需要这样的东西:

click: $root.setPath.bind(/*will be bound to this*/ $root, $data, 
                      $data.tierName(), $parent.identifier())

最后,如果 $data 本身是一个可观察的(不清楚如果它来自您的代码),那么您需要将其称为好吧:

click: $root.setPath.bind($root, $data(), $data().tierName(), $parent.identifier())

还请记住 bind 已在 ECMAScript 5 中引入,因此它可能不会出现在所有浏览器中。所以我可能会这样做:

click: function(){$root.setPath($data, $data.tierName(), $parent.identifier());}

Here 是额外的关于绑定的信息。

Since tierName and identifier are observables, you need to call them to access to their values:

click: $root.setPath.bind($data, $data.tierName(), $parent.identifier())

Also, first argument in bind() will be bound to this in setPath, so I guess you need something like this:

click: $root.setPath.bind(/*will be bound to this*/ $root, $data, 
                      $data.tierName(), $parent.identifier())

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:

click: $root.setPath.bind($root, $data(), $data().tierName(), $parent.identifier())

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:

click: function(){$root.setPath($data, $data.tierName(), $parent.identifier());}

Here is additional info on bind.

我很坚强 2025-01-15 17:46:51

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 want this to be) when your function is executed. So, if you want data 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 calling alert(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 call ko.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文