简体   繁体   中英

How to make this jquery code fire the function itself instead of binding to a button to fire the function?

How to make this jquery code fire the function itself instead of binding to a button to fire the function?

self.ShowWindow= { observable: ko.observable(false) };
self.OnToggleShowWindow = (it) => () => it.observable(!it.observable());
$('#showWindowButton').click(self.OnToggleShowWindow (self.ShowWindow));

Right now I have to create a button and bind it with my code to be able to trigger a function in my react component. What I'm wondering is that, is it possible to just trigger the function without halving to trigger the jquery click?

My ViewModel in the javascript component.

$ViewModel = function (element) {
    var self = this;

    self.ShowWindow= { observable: ko.observable(false) };
    self.OnToggleShowWindow = (it) => () => it.observable(!it.observable());
    self.OnToggleShowWindow (self.ShowWindow);
    $(document).ready(function () {
        self.OnToggleShowWindow(self.ShowWindow)
    };

    self.ShowDialog = function (componentId, componentName) {
       // The fucntion OnToggleShowWindow is supposed to be called here
    };
}

Only outside the ShowDialog function is bound at rendering the page. After what the showdialog is just a function that updates on the already bounded observables.

In my react component I have subscribed to the function, which I now try to retrieve:

this.props.toggleShowWindow.observable.subscribe(this.onToggleShowWindow);

Put () after it instead of passing it to click .

 $('#showWindowButton').click(self.OnToggleShowWindow (self.ShowWindow)); 

becomes:

self.OnToggleShowWindow(self.ShowWindow)()

Don't forget the () after it. Pay close attention to the above line of code and make sure you don't miss any () out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM