jQuery是否解释错了?或Drupal输出错误?

时间:2014-03-04 23:59:34

标签: jquery drupal

我面临着一个特殊的问题。我的Drupal网站使用Jquery 1.4.4版本。

众所周知,Drupal将Drupal.settings json输出到Javascript为:

启用devel_themer模块时

以下是输出

jQuery.extend(Drupal.settings, {
   "basePath":"\/d7\/",
   "pathPrefix":"",
   "ajaxPageState":{
      "theme":"bartik",
      "theme_token":"KC2-1Z9CN5PCxDyAq_W8FGBQBOOnwt8AyoeX1UDBoP8",
      "js":{
         "misc\/jquery.js":1,
         "misc\/jquery.once.js":1,
         "misc\/drupal.js":1,
         "misc\/ui\/jquery.ui.core.min.js":1,
         "misc\/ui\/jquery.ui.widget.min.js":1,
         "misc\/jquery.cookie.js":1,
         "misc\/jquery.form.js":1,
         "misc\/ui\/jquery.ui.mouse.min.js":1,
         "misc\/ui\/jquery.ui.draggable.min.js":1,
         "misc\/ui\/jquery.ui.resizable.min.js":1,
         "misc\/jquery.ba-bbq.js":1,
         "modules\/overlay\/overlay-parent.js":1,
         "misc\/ajax.js":1,
         "misc\/progress.js":1,
         "sites\/all\/modules\/devel_themer\/devel_themer.js":1,
         "sites\/all\/modules\/devel\/krumo\/krumo.js":1,
         "modules\/toolbar\/toolbar.js":1,
         "sites\/all\/modules\/devel\/devel_krumo_path.js":1
      },
      "css":{
         "modules\/system\/system.base.css":1,
         "modules\/system\/system.menus.css":1,
         "modules\/system\/system.messages.css":1,
         "modules\/system\/system.theme.css":1,
         "misc\/ui\/jquery.ui.core.css":1,
         "misc\/ui\/jquery.ui.theme.css":1,
         "misc\/ui\/jquery.ui.resizable.css":1,
         "modules\/overlay\/overlay-parent.css":1,
         "modules\/comment\/comment.css":1,
         "modules\/field\/theme\/field.css":1,
         "modules\/node\/node.css":1,
         "modules\/search\/search.css":1,
         "modules\/user\/user.css":1,
         "sites\/all\/modules\/devel_themer\/devel_themer.css":1,
         "sites\/all\/modules\/devel\/krumo\/skins\/default\/skin.css":1,
         "modules\/shortcut\/shortcut.css":1,
         "modules\/toolbar\/toolbar.css":1,
         "sites\/all\/modules\/devel_themer\/devel_themer_ie_fix.css":1,
         "themes\/bartik\/css\/layout.css":1,
         "themes\/bartik\/css\/style.css":1,
         "themes\/bartik\/css\/colors.css":1,
         "themes\/bartik\/css\/print.css":1,
         "themes\/bartik\/css\/ie.css":1,
         "themes\/bartik\/css\/ie6.css":1
      }
   },
   "thmrStrings":{
      "themer_info":"Themer info",
      "toggle_throbber":" \u003Cimg src=\u0022\/d7\/sites\/all\/modules\/devel_themer\/loader-little.gif\u0022 alt=\u0022loading\u0022 class=\u0022throbber\u0022 width=\u002216\u0022 height=\u002216\u0022 style=\u0022display:none\u0022 \/\u003E",
      "parents":"Parents: ",
      "function_called":"Function called: ",
      "template_called":"Template called: ",
      "candidate_files":"Candidate template files: ",
      "preprocessors":"Preprocess functions: ",
      "processors":"Process functions: ",
      "candidate_functions":"Candidate function names: ",
      "drupal_api_docs":"link to Drupal API documentation",
      "source_link_title":"link to source code",
      "function_arguments":"Function Arguments",
      "template_variables":"Template Variables",
      "file_used":"File used: ",
      "duration":"Duration: ",
      "api_site":"http:\/\/api.drupal.org\/",
      "drupal_version":"7",
      "source_link":"\/d7\/devel\/source?file="
   },
   "thmr_popup":"  \u003Cdiv id=\u0022themer-fixeder\u0022\u003E\n  \u003Cdiv id=\u0022themer-relativer\u0022\u003E\n  \u003Cdiv id=\u0022themer-popup\u0022\u003E\n      \u003Cdiv class=\u0022topper\u0022\u003E\n        \u003Cspan class=\u0022close\u0022\u003EX\u003C\/span\u003E Drupal Themer Information\n      \u003C\/div\u003E\n      \u003Cdiv id=\u0022parents\u0022 class=\u0022row\u0022\u003E\n\n      \u003C\/div\u003E\n      \u003Cdiv class=\u0022info row\u0022\u003E\n        \u003Cdiv class=\u0022starter\u0022\u003EClick on any element to see information about the Drupal theme function or template that created it.\u003C\/div\u003E\n        \u003Cdl\u003E\n          \u003Cdt class=\u0022key-type\u0022\u003E\n\n          \u003C\/dt\u003E\n          \u003Cdd class=\u0022key\u0022\u003E\n\n          \u003C\/dd\u003E\n          \u003Cdiv class=\u0022used\u0022\u003E\n          \u003C\/div\u003E\n          \u003Cdt class=\u0022candidates-type\u0022\u003E\n\n          \u003C\/dt\u003E\n          \u003Cdd class=\u0022candidates\u0022\u003E\n\n          \u003C\/dd\u003E\n\n          \u003Cdt class=\u0022preprocessors-type\u0022\u003E\n\n          \u003C\/dt\u003E\n          \u003Cdd class=\u0022preprocessors\u0022\u003E\n\n          \u003C\/dd\u003E\n          \u003Cdt class=\u0022processors-type\u0022\u003E\n\n          \u003C\/dt\u003E\n          \u003Cdd class=\u0022processors\u0022\u003E\n\n          \u003C\/dd\u003E\n          \u003Cdiv class=\u0022duration\u0022\u003E\u003C\/div\u003E\n        \u003C\/dl\u003E\n      \u003C\/div\u003E\u003C!-- \/info --\u003E\n      \u003Cdiv class=\u0022attributes row\u0022\u003E\n      \u003Cdiv class=\u0022themer-variables\u0022\u003E\u003C\/div\u003E\n      \u003C\/div\u003E\u003C!-- \/attributes --\u003E\n    \u003C\/div\u003E\u003C!-- \/themer-popup --\u003E\n    \u003C\/div\u003E\n    \u003C\/div\u003E",
   "overlay":{
      "paths":{
         "admin":"node\/*\/edit\nnode\/*\/delete\nnode\/*\/revisions\nnode\/*\/revisions\/*\/revert\nnode\/*\/revisions\/*\/delete\nnode\/add\nnode\/add\/*\noverlay\/dismiss-message\nuser\/*\/shortcuts\nadmin\nadmin\/*\nbatch\ntaxonomy\/term\/*\/edit\nuser\/*\/cancel\nuser\/*\/edit\nuser\/*\/edit\/*\ndevel\/*\nnode\/*\/devel\nnode\/*\/devel\/*\ncomment\/*\/devel\ncomment\/*\/devel\/*\nuser\/*\/devel\nuser\/*\/devel\/*\ntaxonomy\/term\/*\/devel\ntaxonomy\/term\/*\/devel\/*",
         "non_admin":"admin\/structure\/block\/demo\/*\nadmin\/reports\/status\/php"
      },
      "pathPrefixes":[

      ],
      "ajaxCallback":"overlay-ajax"
   },
   "tableHeaderOffset":"Drupal.toolbar.height"
});

并且在浏览器控制台中,正如您在下面的屏幕截图中看到的那样Drupal.settings对象有一些奇怪的对象。 enter image description here

有什么奇怪的?

如果我在控制台中写Drupal.settings.thmrStrings,则表示未完成。但根据上述对象,应该有。根据json,它显示了生成一些奇怪的对象,如Drupal.settings.thmr_1Drupal.settings.thmr_2Drupal.settings.thmr_3 .. Drupal.settings.thmr_94,而不是显示这些有意义的对象。这些Drupal.settings.thmr_*对象来自哪里?他们不在json?为什么对象不符合jSON?

您在屏幕截图中看到的错误是由此引起的。 javascript文件devel_themer.js正在访问Drupal.settings.thmrStrings的子对象,但它没有得到它。多数民众赞成在我背后的主要问题。

,当我停用devel_themer模块

以下是输出:

<script type="text/javascript">
<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, {
   "basePath":"\/d7\/",
   "pathPrefix":"",
   "ajaxPageState":{
      "theme":"bartik",
      "theme_token":"KC2-1Z9CN5PCxDyAq_W8FGBQBOOnwt8AyoeX1UDBoP8",
      "js":{
         "misc\/jquery.js":1,
         "misc\/jquery.once.js":1,
         "misc\/drupal.js":1,
         "misc\/ui\/jquery.ui.core.min.js":1,
         "misc\/jquery.ba-bbq.js":1,
         "modules\/overlay\/overlay-parent.js":1,
         "modules\/contextual\/contextual.js":1,
         "misc\/jquery.cookie.js":1,
         "sites\/all\/modules\/devel\/devel_krumo_path.js":1,
         "modules\/toolbar\/toolbar.js":1
      },
      "css":{
         "modules\/system\/system.base.css":1,
         "modules\/system\/system.menus.css":1,
         "modules\/system\/system.messages.css":1,
         "modules\/system\/system.theme.css":1,
         "misc\/ui\/jquery.ui.core.css":1,
         "misc\/ui\/jquery.ui.theme.css":1,
         "modules\/overlay\/overlay-parent.css":1,
         "modules\/contextual\/contextual.css":1,
         "modules\/comment\/comment.css":1,
         "modules\/field\/theme\/field.css":1,
         "modules\/node\/node.css":1,
         "modules\/search\/search.css":1,
         "modules\/user\/user.css":1,
         "modules\/shortcut\/shortcut.css":1,
         "modules\/toolbar\/toolbar.css":1,
         "themes\/bartik\/css\/layout.css":1,
         "themes\/bartik\/css\/style.css":1,
         "themes\/bartik\/css\/colors.css":1,
         "themes\/bartik\/css\/print.css":1,
         "themes\/bartik\/css\/ie.css":1,
         "themes\/bartik\/css\/ie6.css":1
      }
   },
   "overlay":{
      "paths":{
         "admin":"node\/*\/edit\nnode\/*\/delete\nnode\/*\/revisions\nnode\/*\/revisions\/*\/revert\nnode\/*\/revisions\/*\/delete\nnode\/add\nnode\/add\/*\noverlay\/dismiss-message\nuser\/*\/shortcuts\nadmin\nadmin\/*\nbatch\ntaxonomy\/term\/*\/edit\nuser\/*\/cancel\nuser\/*\/edit\nuser\/*\/edit\/*\ndevel\/*\nnode\/*\/devel\nnode\/*\/devel\/*\ncomment\/*\/devel\ncomment\/*\/devel\/*\nuser\/*\/devel\nuser\/*\/devel\/*\ntaxonomy\/term\/*\/devel\ntaxonomy\/term\/*\/devel\/*",
         "non_admin":"admin\/structure\/block\/demo\/*\nadmin\/reports\/status\/php"
      },
      "pathPrefixes":[

      ],
      "ajaxCallback":"overlay-ajax"
   },
   "tableHeaderOffset":"Drupal.toolbar.height"
});
//--><!]]>
</script>

并在浏览器控制台中,您可以看到以下图片Drupal.settings对象被正确解释。我的意思是,对象是按照json的。没有像上面这种情况那样奇怪的对象。

enter image description here

问题:造成这种奇怪行为的原因是什么?如何纠正它?

1 个答案:

答案 0 :(得分:1)

Drupal devel_themer模块在从服务器接收到Drupal.settings JSON响应之后对其进行操作。 source code表示加载页面后devel_themer会修改Drupal.settings值:

if (Drupal.jsEnabled) {
    $(document).ready(function () {
      lastObj = false;
      thmrSpanified = false;
      strs = Drupal.settings.thmrStrings;
      $('body').addClass("thmr_call").attr("id", "thmr_" + Drupal.settings.page_id);
      ...

您应该能够通过在devel_themer之前定义另一个$(document).ready函数来访问未更改的原始Drupal.settings。 document.ready()回调按照它们的注册顺序调用,因此您需要在代码中更早地执行此操作。你已经有了jQuery的依赖,所以用它来深度克隆对象......

if (Drupal.jsEnabled) {
  $(document).ready(function(){
    Drupal.originalSettings = jQuery.extend(true, {}, Drupal.settings);    
    // alert(Drupal.originalSettings.thmrStrings.themer_info);
    // ... do your thing ...
  });
}

在加载Drupal.settings之后和dev_themer模块初始化之前,您需要插入此$(document).ready函数。