问题描述
我正在尝试在rails 3.1应用程序上使用dojo-toolkit作为JS框架,但是我正在努力将dojo需求结构与链轮require和coffeescript相结合。似乎dojo希望在磁盘上的JS文件,但我猜他们是从coffeescript飞行创建的。
I'm trying to use dojo-toolkit as the JS framework on a rails 3.1 app, but I'm struggling to incorporate the dojo require structure with the sprockets require and coffeescript. It seems dojo expects the JS files on the disk, but I guess they're created on the fly from coffeescript.
想知道是否有人有两个想法-s可以共存。
Wondering if anyone has an idea of how the two require-s can co-exist.
推荐答案
我最近不得不用rails 3.1和资产管道安装dojo。以下是我遵循的步骤:
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 /包含Dojo
将dojo SDK放在vendor / assets / javascripts下,以便在其中获得dojo,dijit和dojox文件夹。将其包含在您的模板中:
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!
您可以使用:
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
第一行设置你的。第二个实际上需要Google的dojo。
The first line set your djConfig. The second actually requires dojo from Google. The third is the fallback.
2 /包含您的基本文件
删除应用程序/ assets / javascripts / application.js中的所有require(例如):
Remove all "require" in your app/assets/javascripts/application.js and put something like that (for instance):
dojo.provide("myapp");
3 /玩dojo.require
在1 /中的djConfig中,我设置了modulePaths,将它们自定义为您想要的。在我的例子中,你可以把那两个文件放在你的文件中:
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 /
如果我需要modules / test.js,只需做:
If I want to require modules/test.js, I just do:
dojo.require("modules.test");
4 /使用coffeescript和ERB
只需添加正确的扩展名,然后开始右键,如。
Just add the right extension and start right erb, like described in the Rails documentation.
我希望它可以帮助你!
这篇关于如何使用dojo toolkit与rails 3.1资产管道和coffeescript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!