简体   繁体   English

Javascript模块在Drupal 7中显示并限制了jQuery

[英]Javascript module reveal and scoped jQuery in Drupal 7

Alrightythen! Alrightythen!

So I am busy with some javascript development inside drupal 7 pages. 所以我在drupal 7页中忙于一些javascript开发。 We basically have an MVC that hooks into a drupal page. 基本上,我们有一个MVC可以连接到drupal页面。 Nothing fancy. 没有什么花哨。 Recently I started fixed legacy code that called jQuery.noConflict() to get an instance of jQuery because in drupal 7 jQuery is namespaced and the current dev did not know how to solve the dreaded '$ is not a function' issue without plastering every script with its own 'aliased' jquery instance like so: 最近,我开始修复名为jQuery.noConflict()的旧版代码,以获取jQuery实例,因为在drupal 7中,jQuery是带名称空间的,当前的开发人员不知道如何解决可怕的“ $不是函数”问题,而无需粘贴每个脚本具有自己的“别名” jQuery实例,如下所示:

$xj = jQuery.noConflict();

This causes a hole world of sh!z for any other code assuming $ is still available globally. 假设$在全球仍然可用,那么这对于其他任何代码都会造成sh!z的漏洞。

Now I have cleaned up all the noConflict calls and also wrapped all 'onload code' in this fancy closure: 现在,我清理了所有的noConflict调用,并将所有“ onload代码”包装在这个漂亮的闭包中:

(function ($) {
   // we can now use $ yo....
}(jQuery));

But now, sadly, I am being punished by the universe for using the javascript module reveal pattern. 但是现在,可悲的是,我因使用javascript模块显示模式而受到Universe的惩罚。 Let me try and explain by a code snippet: http://jsfiddle.net/yz41wr2g/ 让我尝试通过代码片段进行解释: http : //jsfiddle.net/yz41wr2g/

Does anyone have experience using namespaced jQuery in the module reveal pattern like this? 有没有人有在模块中使用命名空间jQuery这样的显示模式的经验?

Here is my best attempt: 这是我的最佳尝试:

// drupal does this somewhere just to torture me!
jQuery.noConflict();

//this is my module reveal function/object
var MyModule = function ($) {
    //private members and functions                
    return {
        //public members and functions
        init: function () {
            alert($('.super-success').html());
        }
    };
}(jQuery);

// do 'onload' goodness here
(function ($) {
    // good news, now we have a reference to $
    alert($('.success').html()); // success!        
    MyModule.init();

}(jQuery));

http://jsfiddle.net/6x0exhcp/1/ http://jsfiddle.net/6x0exhcp/1/

Please correct me if i'm wrong =) 如果我错了请指正我=)

You are supposed to wrap all JavaScript code that depends on jQuery in an immediately invoked anonymous function . 您应该将所有依赖jQuery的JavaScript代码包装在立即调用的匿名函数中 This is not a Drupal thing, this is a good practice when using jQuery . 这不是Drupal的事情, 使用jQuery时一个好习惯 Drupal only force you to behave correctly because it behave correctly itself and does not want to prevent you from using another JavaScript library that use $ . Drupal只能强迫您正确执行操作,因为它本身可以正确执行操作,并且不想阻止您使用另一个使用$ JavaScript库。

// drupal does this somewhere to allow me to use another library that uses $
jQuery.noConflict();

(function ($) {
    // $ is now the jQuery object
    //this is my module reveal function/object, added to the global scope.
    window.MyModule = function () {
        //private members and functions                
        return {
            //public members and functions
            init: function () {
                alert($('.super-success').html());
            }
        };
    }();
    // do 'onload' goodness here
    alert($('.success').html()); // success!        
    MyModule.init();
}(jQuery));

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

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