Dojo + Rails 3.2.8 + CoffeeScript

只谈情不闲聊 提交于 2019-12-21 21:11:22

问题


I'm trying to use Dojo Toolkit 1.8 instead JQuery in a Rails 3.2.8 web application, mainly due of the lack of a complete and visually uniform widget based on JQuery. Followed these steps:

  • Unzip dojo, dijit and dojox directories into app/assets/javascripts
  • Changed in application.js //= require_tree . to //= require_directory .
  • Edited application layout (Not unobtrusive yet... just to effect of testing)

views/layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Dojo</title>
  <%= stylesheet_link_tag 'application' %>
  <%= javascript_include_tag 'application' %>
  <%= stylesheet_link_tag '/assets/dijit/themes/claro/claro.css' %>
  <%= javascript_include_tag "dojo/dojo", :'data-dojo-config' => 'async: true' %>
  <%= csrf_meta_tags %>
</head>
<body class='claro'>
  <%= yield %>
  <script>
    require(["dojo/parser", "dojo/ready"], function(parser, ready) {
      ready(function() {
        parser.parse();
      });
    });
  </script>
</body>
</html>
  • Created a controller named home and a template to the index action

views/home/index.html.erb

<input type="text" required="true" name="bday" id="bday" data-dojo-type="dijit/form/DateTextBox" value=<%= localize(Date.today - 21.days) %> />
<button id="button1" type="button" data-dojo-type="dijit/form/Button">Button 1</button>

Ok, Dijit works nice! But when I try to put some dojo code within a CoffeeScript file (assets/javascripts/home.js.coffee), a "ReferenceError: require is not defined" error message is raised on Firebug console. Sample code:

require ["dojo/domReady!"], () ->
  alert('ok')

If I put a //= require dojo/dojo before the code above, it runs, but all dojo modules are loaded (not just domReady) and a "Error: defineAlreadyDefined" is raised on Firebug.

Is there any way to call the require function without having to reload the entire dojo.js or even access a dojo global variable?

Thanks


回答1:


It was a slip of mine, I swapped the javascripts inclusion order. The correct is:

  <%= stylesheet_link_tag '/assets/dijit/themes/claro/claro.css' %>
  <%= javascript_include_tag "dojo/dojo", :'data-dojo-config' => 'async: true' %>
  <%= stylesheet_link_tag 'application' %>
  <%= javascript_include_tag 'application' %>

Dojo wasn't loaded yet when I was trying to require modules within the coffeescript file.

Thank you for your attention.



来源:https://stackoverflow.com/questions/12391840/dojo-rails-3-2-8-coffeescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!