JQuery $(document).trigger(self.MY_FUNCTION)有什么作用?

时间:2014-11-06 22:35:02

标签: javascript jquery html

我在JavaScript对象中找到了以下代码:

;(function(App, $, undefined){

    if (typeof App == 'undefined') window.App = {};


    App.SomeObject = { 

         MY_FUNCTION: 'myfunction',

         myfunction: function(element) {
                var self = App.SomeObject;
                if (element.getSize() < 1) {
                   // case 1
                } else {
                       // case 2
                }

        },

        secondFunction: function() {
            var self = App.SomeObject;
            $(document).trigger(self.MY_FUNCTION);
        }

    }

})(App, jQuery);

在secondFunction中我调用

$(document).trigger(self.MY_FUNCTION);

这条线实际上在做什么?

当我调用$(document).trigger(self.MY_FUNCTION);来自另一个函数的参数是myfunction调用吗?

编辑:我从其他一些代码中复制了这个例子,但我没有理解。我还在代码的其他部分看到以下内容:

myfunction(someElement);
$(document).trigger(self.MY_FUNCTION);

我不知道这意味着什么。

编辑:从JQuery trigger documentation我知道触发器将调用SomeObject的成员函数myfunction,但我不知道正在使用哪些参数。根据我的理解,该方法在没有任何参数的情况下将无法工作,因为if(element.getSize()&lt; 1)将给出错误。

3 个答案:

答案 0 :(得分:1)

jQuery触发器以2种方式使用:

A).trigger(eventType [,extraParameters])

eventType
           Type: String
           A string containing a JavaScript event type, such as click or submit.

extraParameters
           Type: Array or PlainObject
           Additional parameters to pass along to the event handler.

B).trigger(event [,extraParameters])

event
          Type: Event
          A jQuery.Event object.

extraParameters
          Type: Array or PlainObject
          Additional parameters to pass along to the event handler.

所以你可以通过Trigger(原生事件[点击,提交,...] 自定义事件[由jQuery代码设计] 来触发和事件)

<强>编辑:

在您的情况下,您正在尝试触发文档自定义平均值之一,但您的某些部分代码不完整

您必须先将行为指定为ducument,然后使用触发器将其触发。 我为您编写了一个示例,并解释了每一步 HERE

完整代码:

;(function($){
    App=$;

    console.log("Step 1 >> The class of My Object is defining as SomeObject \r\n.")
    App.SomeObject = { 
         BehaviorName: 'TestBehavior',

         TestBehavior: function(target, myElement) {
             console.log("Step 6 >> TestBehavior is Running now...\r\n.");
             var self = App.SomeObject;
             if ($(myElement).width() < 100) {
                 console.log("Step 7 >> element.width ("+$(myElement).width()+") is less than 100px\r\n.");
             } else {
                 console.log("Step 7 >> element.width ("+$(myElement).width()+") is greater than 100px\r\n.");
             }
        },
        secondFunction: function(myElement) {
            console.log("Step 4 >> secondFunction is Running now...\r\n.");
            var self = App.SomeObject;
            console.log("Step 5 >> Firing one of document custom events by TRIGGER and this details:\r\n          EvenType  =  self.BehaviorName (='TestBehavior')    &   otherParameters  =  element\r\n.");
            $(document).trigger(self.BehaviorName,myElement);
        }
    }        
})(jQuery);

console.log("Step 2 >> Assining a new behavior to the page document with this detail:\r\n          eventType = BehaviorName value of SomeObject    &   Handler  =  TestBehavior of SomeObject\r\n.")
$(document).on($.SomeObject.BehaviorName,$.SomeObject.TestBehavior);
console.log("Step 3 >> Calling secondFunction of SomeObject\r\n.")
$.SomeObject.secondFunction($('#sampleElement'));

<强>结果: enter image description here

然后,您的触发器可以通过使用EventType(作为字符串)和extraParameters&lt;&lt;来触发已分配的自定义事件。使用方法A

答案 1 :(得分:0)

首先,self.MY_FUNCTION是什么?看起来它被定义为:MY_FUNCTION: 'myfunction'

那么$(document).trigger('myfunction')做了什么?它会触发myfunction对象上的自定义$(document)事件。

这意味着触发器可以执行任何操作,代码中应该有一个侦听器,如下所示:

$(document).on('myfunction', function() { 
    console.log('custom event triggered!');
});

答案 2 :(得分:0)

来自 .trigger() API文档:

  

执行附加到匹配元素的所有处理程序和行为   对于给定的事件类型。

但是,您的触发器未与事件相关联。也许你打算做以下事情:

window.App = window.App || {};

window.App.SomeObject = {

    myFunction: function (event) {
        console.log('EVENT FIRED!');
        console.log(event);
    },

    init: function () {
        $(document).bind('myEvent', function (event) {
            window.App.SomeObject.myFunction(event);
        });
    }

}

$(function () {
    window.App.SomeObject.init();
});

$(document).trigger('myEvent');

您希望将bind()事件发送到函数。

FIDDLE

<强>更新

您的问题是&#34; JQuery $(文档).trigger(self.MY_FUNCTION)做了什么?&#34;

答案:触发&#34;事件&#34;叫'myFunction'。由于似乎没有任何东西(通过bind())绑定到事件'myFunction',因此触发器什么都不做。此外,您的secondFunction()永远不会被调用。