How to use dojo toolkit with rails 3.1 asset pipeline and coffeescript?

安稳与你 提交于 2019-12-01 05:50:15
karellm

I recently had to install dojo with rails 3.1 and the asset pipeline. Here are the steps I followed to make it work:

1/ Include Dojo

Put the dojo SDK under vendor/assets/javascripts so you get the dojo, dijit and dojox folder in it. Include it in your template:

= javascript_include_tag "dojo/dojo", :'data-dojo-config' => %Q(baseUrl: '/assets/dojo/', modulePaths: {modules: '/assets/modules', widgets: '/assets/widgets'})

Don't forget the leading '/' on assets!

You can use the Google CDN with a fallback:

script var djConfig = { baseUrl: '/assets/dojo/', modulePaths: {modules: '/assets/modules', widgets: 'widgets'} };
= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js"
script ="typeof(dojo) === \"undefined\" && document.write(unescape('%3Cscript src=\"#{asset_path('dojo/dojo')}\"%3E%3C/script%3E'));".html_safe

The first line set your djConfig. The second actually requires dojo from Google. The third is the fallback.


2/ Include your base file

Remove all "require" in your app/assets/javascripts/application.js and put something like that (for instance):

dojo.provide("myapp");

3/ Play with dojo.require

In the djConfig in 1/, I set the modulePaths, custom them to what you want. In my exemple, you would have those two where you can put your files:

  • app/assets/javascripts/modules/
  • app/assets/javascripts/widgets/

If I want to require modules/test.js, I just do:

dojo.require("modules.test");

4/ Use coffeescript and ERB

Just add the right extension and start right erb, like described in the Rails documentation.

I hope it helps you!

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