Dojo + Rails 3.2.8 + CoffeeScript [英] Dojo + Rails 3.2.8 + CoffeeScript

查看:120
本文介绍了Dojo + Rails 3.2.8 + CoffeeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Rails 3.2.8 web应用程序中使用Dojo Toolkit 1.8而不是JQuery,主要是因为缺少一个基于JQuery的完整和可视化的统一部件。遵循以下步骤:

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:


  • 将dojo,dijit和dojox目录解压缩到app / assets / javascripts

  • 已在application.js中更改 // = require_tree。更改为 // = require_directory。

  • 已编辑的应用程式版面配置(非常不引人注意,只是为了测试效果)

  • 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

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>




  • 创建了一个名为home的控制器和一个用于索引操作的模板

    • Created a controller named home and a template to the index action
    • views / home / index.html.erb

      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>
      

      好吧,Dijit工作不错!但是当我尝试在CoffeeScript文件(assets / javascripts / home.js.coffee)中放置一些dojo代码时,会在Firebug控制台上出现一个ReferenceError:require未定义错误消息。示例代码:

      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')
      



      如果我把 / = require dojo / dojo 在上面的代码之前,它运行,但是所有的dojo模块被加载(不仅仅是domReady),并在Firebug上引发一个Error:defineAlreadyDefined。

      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.

      有没有办法调用require函数,而不必重新加载整个dojo.js甚至访问一个dojo全局变量?

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

      感谢

      推荐答案

      这是我的一个滑块,我换了javascripts包含顺序。正确的是:

      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' %>
      

      当我试图在coffeescript文件中需要模块时,Dojo尚未加载。

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

      感谢您的关注。

      这篇关于Dojo + Rails 3.2.8 + CoffeeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆