简体   繁体   English

从Drupal 7中的模块.js文件调用全局JS函数

[英]Call global JS function from module .js file in Drupal 7

With the great help of Sk8erPeter I managed to execute Javascript code on node creation and node update of a certain content type in Drupal 7. Sk8erPeter大力帮助下,我设法在Drupal 7中针对某些内容类型的节点创建和节点更新执行Javascript代码。

My problem now is that I can not call the function FB.api from withing this modules js files. 我现在的问题是,无法通过此模块js文件调用函数FB.api。 Does it have something to do with Javascript namespaces? 它与Javascript名称空间有关吗? Running the FB.api() function from console works fine... 从控制台运行FB.api()函数可以正常工作...

Thanks in advance for any help. 在此先感谢您的帮助。

Nils 尼尔斯

Based on your comments... I'm just watching your testModule.behaviors.js , and it doesn't even look similar to the function that I wrote to you here in the other topic . 根据您的评论,我只是在看您的testModule.behaviors.js ,它看起来与我在另一个主题中写给您的函数看起来并不相似。

Your current code is only like this: 您当前的代码仅是这样的:

FB.api(
    '/me/shareatear:share',
    'post',
    { tear: document.URL },
    function(response) {
       if (!response || response.error) {
          alert('Error occured');
       } else {
          alert('done. ' + response.id);
       }
    });

Where is Drupal.behaviors , and its attach function I showed you earlier? Drupal.behaviors和我之前向您展示的attach功能在哪里? Where are all the other stuffs? 所有其他东西在哪里? :) :)
It's not even surprising that the current code outputs an error, because I think this JavaScript file is included before even defining the FB object, and this way you call this code directly in the header . 当前代码输出错误甚至不足为奇,因为我认为甚至在定义FB对象之前就包含此JavaScript文件,这样您就可以直接在header中调用此代码。

I think your testModule.behaviors.js file should look like this (based on the previous code we were talking about): 我认为您的testModule.behaviors.js文件应如下所示(基于我们之前讨论的代码):

(function ($) {
    Drupal.behaviors.testModule = {
        doitnow: function () {
            alert("A new \"tear\" content has just been added!");

            // change this code to the appropriate one
            FB.api('/me/shareatear:share', 'post', {
                tear: document.URL
            }, function (response) {
                if (!response || response.error) {
                    alert('Error occured');
                } else {
                    alert('done. ' + response.id);
                }
            });

        },

        attach: function (context, settings) {
            try {
                if (settings.testModule.tear_just_added) {
                    this.doitnow();
                }
            } catch (ex) {}
        }
    };
})(jQuery);

So REPLACE your CURRENT content (where you are only calling FB.api without any Drupal-specific behaviors "wrappers"), and change its content to this one. 因此,替换您的当前内容(在这里您仅调用FB.api而没有任何Drupal特定的行为 “包装器”),然后将其内容更改为此内容。


EDIT: 编辑:

OK, try to set the weight of this module higher ONCE with the following db_query() , so its hooks get invoked later than the other modules' hooks. 好的,尝试使用以下db_query()将此模块的权重设置得更高一些,以使其调用比其他模块的调用晚。 After putting these lines in your code, save the file, delete the Drupal cache, and after that, comment out the appropriate line! 将这些行放入代码后,保存文件,删除Drupal缓存,然后注释掉相应的行! It's not needed to run all the time, in every page loads! 不需要一直运行,不需要加载每个页面!

/**
 * Implements hook_init()
 * @see http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_init/7
 */
function testModule_init() {
    // after putting this in your file, save it, delete cache, then COMMENT OUT THE FOLLOWING LINE!!! It should only RUN ONCE (it's enough).
    db_query("UPDATE {system} SET weight = 111 WHERE type = 'module' AND name = 'testModule'");
}

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

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