Loading DataTables with RequireJS in CakePHP: Issue with DebugKit

Loading DataTables with RequireJS in CakePHP: Issue with DebugKit

我已重新编辑此问题,以使其尽可能简洁。我希望这不会打扰你。

我的主要问题是 jquery 插件数据表在我的 requirejs 设置中没有正确加载。 (v1.9.4)

我也在尝试使用 DT_bootstrap(它将数据表扩展到引导程序)。当我运行我的页面时,控制台总是告诉我 DT_bootstrap 失败,因为 $.fn.dataTable 没有定义。问题不能在 DT_bootstrap 中,因为我不需要它来运行数据表,如果我从我的应用程序中删除它,错误仍然是一样的。

我在这里读到 requirejs 还没有准备好正常加载 requirejs,但我发现有些人最终成功实现了它,其中大多数以不同的方式实现。到目前为止,我发现的示例都没有对我有用。

错误:"未捕获的类型错误:无法读取未定义的属性 \\'defaults\\'"(DT_bootstrap.js)

typeof $.fn.dataTable 未定义,它应该是一个函数...

在我决定在我的应用程序中实现 requirejs 之前,我的一个脚本 (general.js) 正在检查是否有任何具有类 "datatable" 的表,当它们存在时,我会异步运行 datatables 脚本,这很好用.

我宁愿保持这种方式,这样我就不会在我的所有应用程序页面中加载数据表代码,但它不起作用。我得到完全相同的错误,就好像我试图用 requirejs 加载它一样。

这是我的"数据主"脚本:

require.config({

  paths: {

   "jquery":"../vendor/jquery/jquery", // 1.9.1

   "jquery.cookie":"../vendor/jquery.cookie/jquery.cookie",

   "bootstrap":"../vendor/bootstrap/docs/assets/js/bootstrap", // 2.3.2

   "bootstrap-timepicker":"../vendor/bootstrap-timepicker/js/bootstrap-timepicker",

   "jqueryui":"jquery-ui-1.10.3.custom.min",

   "datatables":"jquery.dataTables", // 1.9.4

   "datatables-bootstrap":"DT_bootstrap",

   "modernizr":"../vendor/modernizr/modernizr",

   "general":"general"

  },

  shim: {

   "jquery": {

     "exports": 'jQuery'

    },

   "jquery.cookie": {

     "deps": ["jQuery"],

     "exports": 'jQuery'

    },

   "bootstrap": {

     "deps": ['jQuery'],

     "exports": 'jQuery'

    },

   "bootstrap-timepicker" : {

     "deps": ['jQuery', 'bootstrap']

    },

   "datatables": {

     "deps": ['jQuery']

    },

   "datatables-bootstrap": {

     "deps": ['jQuery', 'datatables', 'bootstrap']

    },

   "jqueryui": {

     "deps": ['jQuery']

    },

   "general": {

     "deps": ['jQuery', 'bootstrap']

    }

  }

});



require(

  [

   "modernizr", 

   "jquery", 

   "jquery.cookie", 

   "bootstrap", 

   "bootstrap-timepicker", 

   "jqueryui", 

   "general",

   "datatables",

   "datatables-bootstrap"

  ], 

  function () {

    // console.log('something here');

  }

);
require.config({

  paths: {

   "jquery":"jquery-1.10.2",

   "datatables":"jquery.dataTables-1.9.4.min",

   "DT-bootstrap":"DT_bootstrap"

  },

  shim: {

   "datatables": {

     "deps": ['jquery']

    },

   "DT-bootstrap": {

     "deps": ['datatables']

    }

  }

});



require(["jquery","datatables", 'DT-bootstrap'], function () {



  $('#table_id').dataTable( {

   "aaData": [

      ['Trident', 'Internet Explorer 4.0', 'Win 95+', 4, 'X'],

      ['Trident', 'Internet Explorer 5.0', 'Win 95+', 5, 'C']

    ],

   "aoColumns": [

      {"sTitle":"Engine" },

      {"sTitle":"Browser" },

      {"sTitle":"Platform" },

      {"sTitle":"Version" },

      {"sTitle":"Grade" }

    ]

  });



});

<!DOCTYPE html>

<html>

<head>

<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"/>

<script type="text/javascript" data-main="app.js" src="require.js">

  DataTable Bootstrap

</head>

<body>



  <table id="table_id"/>



</body>

</html>
shim: {

 "jquery.cookie": ["jquery"],

 "bootstrap": ['jquery'],

 "bootstrap-timepicker" : ['jquery', 'bootstrap'],

 "datatables": ['jquery'],

 "datatables-bootstrap": ['datatables', 'bootstrap'],

 "jqueryui": ['jquery'],

 "general": ['jquery', 'bootstrap']

}



require(

  [

   "modernizr", 

   "jquery", 

   "datatables",

   "datatables-bootstrap"

   "jquery.cookie", 

   "bootstrap", 

   "bootstrap-timepicker", 

   "jqueryui", 

   "general"

  ], 

  function () {

    // console.log('something here');

  }

);

还请注意:

  • 这就是我运行 require.js 的方式:<script type="text/javascript" src="/js/require.js" data-main="/js/app.js">(注意 javascript 文件夹的路径以 "/" 开头)

  • 如果我删除 "datatables" 和 "datatables-bootstrap" 我的应用程序运行没有任何错误

  • 在我的 general.js 中,我还有其他异步运行 jquery 插件的条件(除了数据表之外的所有工作)

    示例:如果日历元素存在,则通过 $.getScript()

    加载 jquery 插件日历脚本

  • 用户 dcodesmith 最近试图帮助我(检查他的答案)并要求我在我的应用程序中尝试他的配置,但没有成功。然后我尝试在一个简单的网站中使用它,它适用于那个简单的应用程序,但在我的 cakephp 应用程序中没有发生同样的情况,其中 javascript 文件夹被引用为 "/js"。我发现的主要区别是:在他的应用程序中,所有文件都在同一个文件夹中,而我的应用程序不会发生这种情况(可能与第 1 点有关)。

  • 我也尝试过使用 "exports": 'jQuery.fn.dataTable' 甚至 "exports": 'jQuery.fn.dataTable'"exports": '$.fn.dataTable'... 都没有成功

  • 作为测试,如果我从我的配置中删除两个数据表脚本,然后我运行 $.getScript() 文件加载成功,但 jquery 插件仍未定义($.fn.dataTable),因此我仍然不能使用它


  • 是的,所以我所做的就是从下往上开始,让裸机配置正常工作。

    app.js

    require.config({
    
      paths: {
    
       "jquery":"../vendor/jquery/jquery", // 1.9.1
    
       "jquery.cookie":"../vendor/jquery.cookie/jquery.cookie",
    
       "bootstrap":"../vendor/bootstrap/docs/assets/js/bootstrap", // 2.3.2
    
       "bootstrap-timepicker":"../vendor/bootstrap-timepicker/js/bootstrap-timepicker",
    
       "jqueryui":"jquery-ui-1.10.3.custom.min",
    
       "datatables":"jquery.dataTables", // 1.9.4
    
       "datatables-bootstrap":"DT_bootstrap",
    
       "modernizr":"../vendor/modernizr/modernizr",
    
       "general":"general"
    
      },
    
      shim: {
    
       "jquery": {
    
         "exports": 'jQuery'
    
        },
    
       "jquery.cookie": {
    
         "deps": ["jQuery"],
    
         "exports": 'jQuery'
    
        },
    
       "bootstrap": {
    
         "deps": ['jQuery'],
    
         "exports": 'jQuery'
    
        },
    
       "bootstrap-timepicker" : {
    
         "deps": ['jQuery', 'bootstrap']
    
        },
    
       "datatables": {
    
         "deps": ['jQuery']
    
        },
    
       "datatables-bootstrap": {
    
         "deps": ['jQuery', 'datatables', 'bootstrap']
    
        },
    
       "jqueryui": {
    
         "deps": ['jQuery']
    
        },
    
       "general": {
    
         "deps": ['jQuery', 'bootstrap']
    
        }
    
      }
    
    });
    
    
    
    require(
    
      [
    
       "modernizr", 
    
       "jquery", 
    
       "jquery.cookie", 
    
       "bootstrap", 
    
       "bootstrap-timepicker", 
    
       "jqueryui", 
    
       "general",
    
       "datatables",
    
       "datatables-bootstrap"
    
      ], 
    
      function () {
    
        // console.log('something here');
    
      }
    
    );
    require.config({
    
      paths: {
    
       "jquery":"jquery-1.10.2",
    
       "datatables":"jquery.dataTables-1.9.4.min",
    
       "DT-bootstrap":"DT_bootstrap"
    
      },
    
      shim: {
    
       "datatables": {
    
         "deps": ['jquery']
    
        },
    
       "DT-bootstrap": {
    
         "deps": ['datatables']
    
        }
    
      }
    
    });
    
    
    
    require(["jquery","datatables", 'DT-bootstrap'], function () {
    
    
    
      $('#table_id').dataTable( {
    
       "aaData": [
    
          ['Trident', 'Internet Explorer 4.0', 'Win 95+', 4, 'X'],
    
          ['Trident', 'Internet Explorer 5.0', 'Win 95+', 5, 'C']
    
        ],
    
       "aoColumns": [
    
          {"sTitle":"Engine" },
    
          {"sTitle":"Browser" },
    
          {"sTitle":"Platform" },
    
          {"sTitle":"Version" },
    
          {"sTitle":"Grade" }
    
        ]
    
      });
    
    
    
    });
    
    <!DOCTYPE html>
    
    <html>
    
    <head>
    
    <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"/>
    
    <script type="text/javascript" data-main="app.js" src="require.js">
    
      DataTable Bootstrap
    
    </head>
    
    <body>
    
    
    
      <table id="table_id"/>
    
    
    
    </body>
    
    </html>
    shim: {
    
     "jquery.cookie": ["jquery"],
    
     "bootstrap": ['jquery'],
    
     "bootstrap-timepicker" : ['jquery', 'bootstrap'],
    
     "datatables": ['jquery'],
    
     "datatables-bootstrap": ['datatables', 'bootstrap'],
    
     "jqueryui": ['jquery'],
    
     "general": ['jquery', 'bootstrap']
    
    }
    
    
    
    require(
    
      [
    
       "modernizr", 
    
       "jquery", 
    
       "datatables",
    
       "datatables-bootstrap"
    
       "jquery.cookie", 
    
       "bootstrap", 
    
       "bootstrap-timepicker", 
    
       "jqueryui", 
    
       "general"
    
      ], 
    
      function () {
    
        // console.log('something here');
    
      }
    
    );

    index.html

    require.config({
    
      paths: {
    
       "jquery":"../vendor/jquery/jquery", // 1.9.1
    
       "jquery.cookie":"../vendor/jquery.cookie/jquery.cookie",
    
       "bootstrap":"../vendor/bootstrap/docs/assets/js/bootstrap", // 2.3.2
    
       "bootstrap-timepicker":"../vendor/bootstrap-timepicker/js/bootstrap-timepicker",
    
       "jqueryui":"jquery-ui-1.10.3.custom.min",
    
       "datatables":"jquery.dataTables", // 1.9.4
    
       "datatables-bootstrap":"DT_bootstrap",
    
       "modernizr":"../vendor/modernizr/modernizr",
    
       "general":"general"
    
      },
    
      shim: {
    
       "jquery": {
    
         "exports": 'jQuery'
    
        },
    
       "jquery.cookie": {
    
         "deps": ["jQuery"],
    
         "exports": 'jQuery'
    
        },
    
       "bootstrap": {
    
         "deps": ['jQuery'],
    
         "exports": 'jQuery'
    
        },
    
       "bootstrap-timepicker" : {
    
         "deps": ['jQuery', 'bootstrap']
    
        },
    
       "datatables": {
    
         "deps": ['jQuery']
    
        },
    
       "datatables-bootstrap": {
    
         "deps": ['jQuery', 'datatables', 'bootstrap']
    
        },
    
       "jqueryui": {
    
         "deps": ['jQuery']
    
        },
    
       "general": {
    
         "deps": ['jQuery', 'bootstrap']
    
        }
    
      }
    
    });
    
    
    
    require(
    
      [
    
       "modernizr", 
    
       "jquery", 
    
       "jquery.cookie", 
    
       "bootstrap", 
    
       "bootstrap-timepicker", 
    
       "jqueryui", 
    
       "general",
    
       "datatables",
    
       "datatables-bootstrap"
    
      ], 
    
      function () {
    
        // console.log('something here');
    
      }
    
    );
    require.config({
    
      paths: {
    
       "jquery":"jquery-1.10.2",
    
       "datatables":"jquery.dataTables-1.9.4.min",
    
       "DT-bootstrap":"DT_bootstrap"
    
      },
    
      shim: {
    
       "datatables": {
    
         "deps": ['jquery']
    
        },
    
       "DT-bootstrap": {
    
         "deps": ['datatables']
    
        }
    
      }
    
    });
    
    
    
    require(["jquery","datatables", 'DT-bootstrap'], function () {
    
    
    
      $('#table_id').dataTable( {
    
       "aaData": [
    
          ['Trident', 'Internet Explorer 4.0', 'Win 95+', 4, 'X'],
    
          ['Trident', 'Internet Explorer 5.0', 'Win 95+', 5, 'C']
    
        ],
    
       "aoColumns": [
    
          {"sTitle":"Engine" },
    
          {"sTitle":"Browser" },
    
          {"sTitle":"Platform" },
    
          {"sTitle":"Version" },
    
          {"sTitle":"Grade" }
    
        ]
    
      });
    
    
    
    });
    
    <!DOCTYPE html>
    
    <html>
    
    <head>
    
    <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"/>
    
    <script type="text/javascript" data-main="app.js" src="require.js">
    
      DataTable Bootstrap
    
    </head>
    
    <body>
    
    
    
      <table id="table_id"/>
    
    
    
    </body>
    
    </html>
    shim: {
    
     "jquery.cookie": ["jquery"],
    
     "bootstrap": ['jquery'],
    
     "bootstrap-timepicker" : ['jquery', 'bootstrap'],
    
     "datatables": ['jquery'],
    
     "datatables-bootstrap": ['datatables', 'bootstrap'],
    
     "jqueryui": ['jquery'],
    
     "general": ['jquery', 'bootstrap']
    
    }
    
    
    
    require(
    
      [
    
       "modernizr", 
    
       "jquery", 
    
       "datatables",
    
       "datatables-bootstrap"
    
       "jquery.cookie", 
    
       "bootstrap", 
    
       "bootstrap-timepicker", 
    
       "jqueryui", 
    
       "general"
    
      ], 
    
      function () {
    
        // console.log('something here');
    
      }
    
    );

    文件夹结构

    关于 javascript:Loading DataTables with RequireJS in CakePHP: Issue with DebugKit

    更新:使用下面的垫片并要求下面的声明

    require.config({
    
      paths: {
    
       "jquery":"../vendor/jquery/jquery", // 1.9.1
    
       "jquery.cookie":"../vendor/jquery.cookie/jquery.cookie",
    
       "bootstrap":"../vendor/bootstrap/docs/assets/js/bootstrap", // 2.3.2
    
       "bootstrap-timepicker":"../vendor/bootstrap-timepicker/js/bootstrap-timepicker",
    
       "jqueryui":"jquery-ui-1.10.3.custom.min",
    
       "datatables":"jquery.dataTables", // 1.9.4
    
       "datatables-bootstrap":"DT_bootstrap",
    
       "modernizr":"../vendor/modernizr/modernizr",
    
       "general":"general"
    
      },
    
      shim: {
    
       "jquery": {
    
         "exports": 'jQuery'
    
        },
    
       "jquery.cookie": {
    
         "deps": ["jQuery"],
    
         "exports": 'jQuery'
    
        },
    
       "bootstrap": {
    
         "deps": ['jQuery'],
    
         "exports": 'jQuery'
    
        },
    
       "bootstrap-timepicker" : {
    
         "deps": ['jQuery', 'bootstrap']
    
        },
    
       "datatables": {
    
         "deps": ['jQuery']
    
        },
    
       "datatables-bootstrap": {
    
         "deps": ['jQuery', 'datatables', 'bootstrap']
    
        },
    
       "jqueryui": {
    
         "deps": ['jQuery']
    
        },
    
       "general": {
    
         "deps": ['jQuery', 'bootstrap']
    
        }
    
      }
    
    });
    
    
    
    require(
    
      [
    
       "modernizr", 
    
       "jquery", 
    
       "jquery.cookie", 
    
       "bootstrap", 
    
       "bootstrap-timepicker", 
    
       "jqueryui", 
    
       "general",
    
       "datatables",
    
       "datatables-bootstrap"
    
      ], 
    
      function () {
    
        // console.log('something here');
    
      }
    
    );
    require.config({
    
      paths: {
    
       "jquery":"jquery-1.10.2",
    
       "datatables":"jquery.dataTables-1.9.4.min",
    
       "DT-bootstrap":"DT_bootstrap"
    
      },
    
      shim: {
    
       "datatables": {
    
         "deps": ['jquery']
    
        },
    
       "DT-bootstrap": {
    
         "deps": ['datatables']
    
        }
    
      }
    
    });
    
    
    
    require(["jquery","datatables", 'DT-bootstrap'], function () {
    
    
    
      $('#table_id').dataTable( {
    
       "aaData": [
    
          ['Trident', 'Internet Explorer 4.0', 'Win 95+', 4, 'X'],
    
          ['Trident', 'Internet Explorer 5.0', 'Win 95+', 5, 'C']
    
        ],
    
       "aoColumns": [
    
          {"sTitle":"Engine" },
    
          {"sTitle":"Browser" },
    
          {"sTitle":"Platform" },
    
          {"sTitle":"Version" },
    
          {"sTitle":"Grade" }
    
        ]
    
      });
    
    
    
    });
    
    <!DOCTYPE html>
    
    <html>
    
    <head>
    
    <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"/>
    
    <script type="text/javascript" data-main="app.js" src="require.js">
    
      DataTable Bootstrap
    
    </head>
    
    <body>
    
    
    
      <table id="table_id"/>
    
    
    
    </body>
    
    </html>
    shim: {
    
     "jquery.cookie": ["jquery"],
    
     "bootstrap": ['jquery'],
    
     "bootstrap-timepicker" : ['jquery', 'bootstrap'],
    
     "datatables": ['jquery'],
    
     "datatables-bootstrap": ['datatables', 'bootstrap'],
    
     "jqueryui": ['jquery'],
    
     "general": ['jquery', 'bootstrap']
    
    }
    
    
    
    require(
    
      [
    
       "modernizr", 
    
       "jquery", 
    
       "datatables",
    
       "datatables-bootstrap"
    
       "jquery.cookie", 
    
       "bootstrap", 
    
       "bootstrap-timepicker", 
    
       "jqueryui", 
    
       "general"
    
      ], 
    
      function () {
    
        // console.log('something here');
    
      }
    
    );

    我终于找到了问题的根源。

    我重新创建了一个与我的 cakephp 应用程序具有相同类型的路由和文件夹的网站,我终于找到了一些东西。

    我在 CakePHP 中使用了一个名为 DebugKit 的调试插件,该插件在文档末尾附加了 2 个脚本。其中之一是 jQuery 1.8.1 和插件脚本,它基本上是一个类似于水平导航的工具栏。

    我总是被告知不要担心删除这个 jQuery 实例,因为它是以非冲突方式加载的,碰巧一旦我禁用它,我的 requirejs 配置终于可以按照我的意愿使用插件数据表了!

    我不知道为什么这个冲突的确切原因,但我很确定它来自这部分代码:

    https://github.com/cakephp/debug_kit/blob/master/webroot/js/js_debug_toolbar.js#L59-73

    我以前从未注意到这一点,因为我只在管理部分使用数据表插件,并且当我以管理员身份登录时,php 调试器插件始终处于打开状态。

    我将更改标题以包含 cakephp,可能对遇到相同问题的人有用


相关推荐

  • Spring部署设置openshift

    Springdeploymentsettingsopenshift我有一个问题让我抓狂了三天。我根据OpenShift帐户上的教程部署了spring-eap6-quickstart代码。我已配置调试选项,并且已将Eclipse工作区与OpehShift服务器同步-服务器上的一切工作正常,但在Eclipse中出现无法消除的错误。我有这个错误:cvc-complex-type.2.4.a:Invali…
    2025-04-161
  • 检查Java中正则表达式中模式的第n次出现

    CheckfornthoccurrenceofpatterninregularexpressioninJava本问题已经有最佳答案,请猛点这里访问。我想使用Java正则表达式检查输入字符串中特定模式的第n次出现。你能建议怎么做吗?这应该可以工作:MatchResultfindNthOccurance(intn,Patternp,CharSequencesrc){Matcherm=p.matcher…
    2025-04-161
  • 如何让 JTable 停留在已编辑的单元格上

    HowtohaveJTablestayingontheeditedcell如果有人编辑JTable的单元格内容并按Enter,则内容会被修改并且表格选择会移动到下一行。是否可以禁止JTable在单元格编辑后转到下一行?原因是我的程序使用ListSelectionListener在单元格选择上同步了其他一些小部件,并且我不想在编辑当前单元格后选择下一行。Enter的默认绑定是名为selectNext…
    2025-04-161
  • Weblogic 12c 部署

    Weblogic12cdeploy我正在尝试将我的应用程序从Tomcat迁移到Weblogic12.2.1.3.0。我能够毫无错误地部署应用程序,但我遇到了与持久性提供程序相关的运行时错误。这是堆栈跟踪:javax.validation.ValidationException:CalltoTraversableResolver.isReachable()threwanexceptionatorg.…
    2025-04-161
  • Resteasy Content-Type 默认值

    ResteasyContent-Typedefaults我正在使用Resteasy编写一个可以返回JSON和XML的应用程序,但可以选择默认为XML。这是我的方法:@GET@Path("/content")@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})publicStringcontentListRequestXm…
    2025-04-161
  • 代码不会停止运行,在 Java 中

    thecodedoesn'tstoprunning,inJava我正在用Java解决项目Euler中的问题10,即"Thesumoftheprimesbelow10is2+3+5+7=17.Findthesumofalltheprimesbelowtwomillion."我的代码是packageprojecteuler_1;importjava.math.BigInteger;importjava…
    2025-04-161
  • Out of memory java heap space

    Outofmemoryjavaheapspace我正在尝试将大量文件从服务器发送到多个客户端。当我尝试发送大小为700mb的文件时,它显示了"OutOfMemoryjavaheapspace"错误。我正在使用Netbeans7.1.2版本。我还在属性中尝试了VMoption。但仍然发生同样的错误。我认为阅读整个文件存在一些问题。下面的代码最多可用于300mb。请给我一些建议。提前致谢publicc…
    2025-04-161
  • Log4j 记录到共享日志文件

    Log4jLoggingtoaSharedLogFile有没有办法将log4j日志记录事件写入也被其他应用程序写入的日志文件。其他应用程序可以是非Java应用程序。有什么缺点?锁定问题?格式化?Log4j有一个SocketAppender,它将向服务发送事件,您可以自己实现或使用与Log4j捆绑的简单实现。它还支持syslogd和Windows事件日志,这对于尝试将日志输出与来自非Java应用程序…
    2025-04-161