简体   繁体   English

jQuery小部件触发的事件未得到处理或触发

[英]Jquery widget triggered event not being handled or fired

I have a very simple widget outlined that appears to fire an event via _trigger that I cant seem to bind to: 我概述了一个非常简单的小部件,该小部件似乎通过_trigger触发了一个我似乎无法绑定到的事件:

; (function ($) {
    $.widget('testspace.ftpWidget', {
        options: {},
        widgetEventPrefix: 'ftpwidget:',
        _create: function () {
            var that = this;
            that.paused = ko.observable(false);
        },
        _triggerPlayerInitialized: function () {
            console.log("player initialized");
            this._trigger("testeventname", null, null);
            console.log("player testeventname supposedly fired");
        },
        pause: function () {
            this.paused(!this.paused());
            console.log("player paused");
            this._triggerPlayerInitialized();
        },
        _destroy: function () { },
        _setOption: function (key, value) { }
    });
}(jQuery));

In an html file: 在html文件中:

<script>
   jQuery(document).ready(function ($) {
      $('#videoA').ftpWidget();
      var widget = $('#videoA').data("testspace-ftpWidget");
      console.log(widget);
      $(widget).on("ftpwidget:testeventname", function (event, data) {
          console.log("widget initialized");
      });
      widget.pause();
 });

And console output: 和控制台输出:

控制台输出

Why is this event not being handled / or is it not even being fired? 为什么不处理此事件,或者甚至不触发该事件? How can I debug this? 我该如何调试?

Update: 更新:

This answer , though not my problem, gave me a hint. 这个答案虽然不是我的问题,但给了我一个提示。 The OP is calling the on binding from an element body . OP正在从元素body调用on绑定。 So I changed 所以我改变了

 $(widget).on("ftpwidget:testeventname", function (event, data) {

to

 $("body").on("ftpwidget:testeventname", function (event, data) {

and it worked, so does 它奏效了

 $("#video").on("ftpwidget:testeventname", function (event, data) {

So now I have it working but don't really understand why. 因此,现在我可以正常使用了,但并不真正理解为什么。 Is it a scope "thing"? 它是范围“事物”吗? I was thinking 我刚在想

"I want to register for the testeventname on widget " “我想在widget上注册testeventname

What's going on here? 这里发生了什么?

_trigger itself can actually call your function. _trigger本身实际上可以调用您的函数。 In the first parameter you're specifying the name of the callback function you want to trigger. 在第一个参数中,您要指定要触发的回调函数的名称。 So if you were to change: 因此,如果要更改:

$('#videoA').ftpWidget();

to

$('#videoA').ftpWidget({
  testeventname: function (event, data) {
    console.log("widget initialized");
  }
});

that would also work. 那也可以。

Jquery-UI: How to Use the Widget Factory Jquery-UI:如何使用小部件工厂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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