如何防止使用参数在页面加载上被调用?

发布于 2025-01-25 19:00:18 字数 1118 浏览 2 评论 0原文

我有一个单击按钮后调用的函数。 现在看来,

<button class=".." data-bind="click: $root.openModal">

我正在尝试将参数传递给此功能。导致此代码

<button class=".." data-bind="click: $root.openModal(Object.Keys(params))">

参数将成功传递,但是现在,每当我加载页面时,即使在单击按钮之前,都调用了Open -Modal函数。即使我离开'()'而不是 openmodal(object.keys(params)),也会发生同样的情况。

函数本身,看起来这样,

self.openModal = function (keys) {
    popupObservable(true);

    self.modal = $.openModalDefault({
        url: '#js-confirmation-dialog-template',
        className: 'doc',
        onLoad: function () {
            popupObservable(false);
            if (!$(selectors.confirmationModal)[0]) {
                return;
            }
            var viewModel = new ConfirmationDialogViewModel(function () {
                self.confirm(keys);
                self.modal.close();
            }, "This part is irrelevant");
            ko.applyBindings(viewModel, $(selectors.confirmationModal)[0]);
        }
    });
};

OpenModal和OpenModal()有什么区别,如何将参数传递给此函数而不在页面加载上触发它?

I have a function that is called after button is clicked. it looks like this

<button class=".." data-bind="click: $root.openModal">

Now I am trying to pass parameters to this function. Resulting in this code

<button class=".." data-bind="click: $root.openModal(Object.Keys(params))">

Parameters are passed successfully, but now whenever I load the page, before even clicking the button, openModal function is called. Same happens even if I leave '()' instead of openModal(Object.Keys(params)).

function itself, looks like this

self.openModal = function (keys) {
    popupObservable(true);

    self.modal = $.openModalDefault({
        url: '#js-confirmation-dialog-template',
        className: 'doc',
        onLoad: function () {
            popupObservable(false);
            if (!$(selectors.confirmationModal)[0]) {
                return;
            }
            var viewModel = new ConfirmationDialogViewModel(function () {
                self.confirm(keys);
                self.modal.close();
            }, "This part is irrelevant");
            ko.applyBindings(viewModel, $(selectors.confirmationModal)[0]);
        }
    });
};

What is the difference between openModal and openModal() and how do I pass parameters to this function without triggering it on page load?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挽你眉间 2025-02-01 19:00:18

那是因为您在data-bind =“单击:$ root.openmodal(object.keys(params))上调用函数”您想用这些参数动态构建功能并执行该功能点击。

self.openModalWithParam = function (keys) {

// keys come from the outer function and returns a function for those values

return function () {
    popupObservable(true);

    self.modal = $.openModalDefault({
        url: '#js-confirmation-dialog-template',
        className: 'doc',
        onLoad: function () {
            popupObservable(false);
            if (!$(selectors.confirmationModal)[0]) {
                return;
            }
            var viewModel = new ConfirmationDialogViewModel(function () {
                self.confirm(keys);
                self.modal.close();
            }, "This part is irrelevant");
            ko.applyBindings(viewModel, $(selectors.confirmationModal)[0]);
        }
    });
 };
};

That is because you are invoking the function on data-bind="click: $root.openModal(Object.Keys(params))" instead you want to construct a function dynamically with those parameters and execute that on click.

self.openModalWithParam = function (keys) {

// keys come from the outer function and returns a function for those values

return function () {
    popupObservable(true);

    self.modal = $.openModalDefault({
        url: '#js-confirmation-dialog-template',
        className: 'doc',
        onLoad: function () {
            popupObservable(false);
            if (!$(selectors.confirmationModal)[0]) {
                return;
            }
            var viewModel = new ConfirmationDialogViewModel(function () {
                self.confirm(keys);
                self.modal.close();
            }, "This part is irrelevant");
            ko.applyBindings(viewModel, $(selectors.confirmationModal)[0]);
        }
    });
 };
};
﹏雨一样淡蓝的深情 2025-02-01 19:00:18

另外,您只需将函数传递到单击处理程序时即可传递:

&lt; button data-bind =“ click:()=&gt; $ root.openmodal(object.keys(params))”&gt;

我个人更喜欢,因为这意味着您不必复制Open -Modal函数。

Alternatively, you could just pass a function literal as the click handler:

<button data-bind="click: () => $root.openModal(Object.Keys(params))">

Which I would personally prefer because it means you don't have to duplicate your openModal function.

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